我正在尝试将数据插入表中,我看到的代码片段似乎适用于那个人,但对我来说!我不知道我做错了什么,因为我不知道 asp.net 的数据库处理。有人可以告诉我代码有什么问题吗?
public partial class CompanyLogin : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = @".\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\VCtemps.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
string sql = "insert into company values(@CompName, @BusinessType, @Pword)";
OdbcCommand cmd = new OdbcCommand(sql);
string CompName = txtCompName.Text;
string BusinessType = DropDownList1.Text;
string Pword = txtPassword.Text;
cmd.Connection = conn;
cmd.CommandText = "insert into company(CompName, BusinessType, Pword) Values(@CompName,@BusinessType,@Pword);";
cmd.Parameters.AddWithValue("@CompName",SqlDbType.VarChar);
cmd.Parameters.AddWithValue("@BusinessType",SqlDbType.VarChar);
cmd.Parameters.AddWithValue("@Pword",SqlDbType.VarChar);
cmd.ExecuteNonQuery();
conn.Close();
txtCompName.Text = "";
txtPassword.Text = "";
DropDownList1.Text = "";
}
}
感谢你们,我修复了代码,但是当我运行它或单击注册按钮时,我收到以下错误
ExecuteNonQuery 需要一个开放且可用的连接。连接的当前状态为关闭