ウィンドウアプリケーションで、データベースを操作しようとしています.app.configに接続文字列を書き込む方法. 以下は、app.config の接続文字列です。
<configuration>
<appSettings >
<add key ="mycon"
value ="server=192*****;database=*****;
uid=**;pwd=*****;"/>
</appSettings>
</configuration>
データベースに接続するコード:
SqlConnection con = new SqlConnection(
ConfigurationSettings.AppSettings["mycon"].ToString()); con.Open();
SqlCommand cmd = new SqlCommand("Usp_chat_login", con);
cmd.CommandType = CommandType.StoredProcedure ;
cmd.Parameters.Add("@userid", SqlDbType.VarChar, 20);
cmd.Parameters["@userid"].Value = textBox1.Text;
cmd.Parameters.Add("@password", SqlDbType.VarChar, 20);
cmd.Parameters["@password"].Value = textBox2.Text;
int reslt = cmd.ExecuteNonQuery(); con.Close();
if (reslt > 0)
{
MessageBox.Show("Yes");
}
else
{
MessageBox.Show("No");
}
reslt=-1
正しい資格情報を渡しても、取得するたびに