1

这是我从文本字段插入数据库的代码。

SqlConnection Connection = new SqlConnection("Data Source=ESHA\\SQLEXPRESS;Initial Catalog=Gen_Lic;User ID=sa;Password=sa@");           

SqlCommand Command = Connection.CreateCommand();

try
{
// Open Connection             
Connection.Open();
////Console.WriteLine("Connection Opened");
// Create INSERT statement with named parameters             
Command.CommandText = "INSERT  INTO Gen_Lic(Lic_No, UserID, Org, UserName, SolType, Version, Lic_Type, Meap_Supp, Lic_From, Lic_To, Supp_From, Supp_To, Max_User, Max_Mach, Mach_IP, Mach_MAC) VALUES (@Lic_No, @UserID, @Org, @UserName, @SolType, @Version, @Lic_Type, @Meap_Supp, @Lic_From, @Lic_To, @Supp_From, @Supp_To, @Max_User, @Max_Mach, @Mach_IP, @Mach_MAC)";
// Add Parameters to Command Parameters collection  

Command.Parameters.AddWithValue("@Lic_No", txtLNo.Text);
Command.Parameters.AddWithValue("@UserID", txtUID.Text);
Command.Parameters.AddWithValue("@Org", txtOrg.Text);
Command.Parameters.AddWithValue("@UserName", txtUName.Text);
Command.Parameters.AddWithValue("@SolType", txtSType.Text);
Command.Parameters.AddWithValue("@Version", txtVer.Text);
Command.Parameters.AddWithValue("@Lic_Type", drpLType.SelectedItem.Text);
Command.Parameters.AddWithValue("@Meap_Supp", rdoMeapSupport.SelectedValue.ToString());
Command.Parameters.AddWithValue("@Lic_From", lblLFrom.Text);
Command.Parameters.AddWithValue("@Lic_To", lblLTo.Text);
Command.Parameters.AddWithValue("@Supp_From", lblSuppFrom.Text);
Command.Parameters.AddWithValue("@Supp_To", lblSuppTo.Text);
Command.Parameters.AddWithValue("@Max_User", txtMaxUsr.Text);
Command.Parameters.AddWithValue("@Max_Mach", txtMaxMach.Text);
Command.Parameters.AddWithValue("@Mach_IP", txtMachIP.Text);
Command.Parameters.AddWithValue("@Mach_MAC", txtMachMac.Text);

Connection.Close();

}

现在的问题是代码工作正常,但值没有插入到数据库中。此外,当我在连接形成开始时应用断点时,会打开一个新的空白 IE 窗口。

有人可以指导我哪里出错了吗?

4

2 回答 2

6

我认为,您缺少以下内容:

Command.ExecuteNonQuery(); 

之前Connection.Close()

于 2012-11-27T07:18:32.830 回答
1

请在数据库中再次运行此查询

Command.ExecuteNonQuery();

在关闭连接之前

于 2012-11-27T07:18:45.917 回答