c#ExecuteNonQuery: Connection 属性尚未初始化。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data;
using System.Data.SqlClient;

public partial class costomera : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection("server=localhost;database=BBT;user id=sa;pwd=123");

SqlDataAdapter sda = new SqlDataAdapter();

SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();

protected void Page_Load(object sender, EventArgs e)
{

}
protected void 确定_Click(object sender, EventArgs e)
{
cmd.CommandText = "insert into costomera(customers_id,Placename,credit) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox6.Text + "')";
SqlCommand myCommand = new SqlCommand();

conn.Open();

cmd.ExecuteNonQuery();
conn.Close();

}
protected void Button2_Click(object sender, EventArgs e)
{
cmd.CommandText = "update comstomera(customers_id,Placename,credit)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox6.Text + "')";

conn.Open();

cmd.ExecuteNonQuery();
conn.Close();

}

}
最新回答
扯淡的嘱咐

2024-11-24 07:48:24

楼主没把 SqlConnection 对象传给SqlCommand。
加一句
cmd.Connection=conn;
恶魔女狱长

2024-11-24 07:37:25

一楼说的没错,支持!