Sir, 我在SQL Server中的数据库有一些问题。 我的项目是与登录方法的桌面应用程序.... 我的程序是用C#编写的,正常执行,因为我想在我的查询中没有错误... 但是数据不会存储回程序插入或更新的数据库。 ..我的数据库是由Visual Studio 2010作为数据文件夹中的单独的mdf文件创建的... 所以,请在此帮助我。下面是一个示例代码:SQL Server数据库与我在c中的项目同步#
private void btnLogin_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection cn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database_File.mdf;Integrated Security=True;User Instance=True"))
{
using (SqlCommand cmd = new SqlCommand("select * from table_users where user_name='" + txtBoxUserName.Text + "' and user_password='" + txtBoxUserPass.Text + "' ",cn))
{
cn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr["user_name"].ToString() == txtBoxUserName.Text && dr["user_password"].ToString() == txtBoxUserPass.Text)
{
...
...
try
{
using (SqlCommand cmd1 = new SqlCommand("insert into table_user(id,user_name,user_status) values(" + key + "," + active_user_name + "',"+ user_status + ")",cn))
{
cmd1.ExecuteNonQuery();
Thread form_name = new Thread(new ThreadStart(thread_Proc_form_name));
form_name.Start();
}
}
catch { throw;}
}
}
}
}
}
catch { throw;}
}
程序以我想要的方式运行,但是当我查看数据库表时未保存更改。例如:当我登录时,登录信息在下次运行时不会显示在数据库中。 登记册的情况也是如此......每次运行应用程序时,我都必须重复注册。我不知道错误在哪里。请帮我先生我要去的地方错了....
感谢&问候 SAURABH马哈詹
请注意,TRY CATCH {throw;}是一个默认行为。您可以删除这些,它不会改变任何东西到您的应用程序 – Mathieu 2012-03-30 02:48:31
谢谢先生...但使用try catch只是一个小应用程序的选项...我们使用它或它不会影响这个问题... – Saurabh 2012-03-30 02:52:40
请点在代码假设更新数据库的地方,因为我没有看到它!如果它是cmd1,它是一个空插入查询! – Mathieu 2012-03-30 02:53:35