private void Form1_Load(object sender, System.EventArgs e)
{
dat.Columns.Add("id");
dat.Columns.Add("username");
dat.Columns.Add("usepwd");
this.dataGrid1.DataSource=dat;
}
private void button2_Click(object sender, System.EventArgs e)
{
string str="server=localhost;uid=sa;pwd=;database=login";
conn=new SqlConnection(str);
SqlCommand cm=new SqlCommand();
cm.Connection=conn;
cm.CommandText="insert into login values(@id,@username,@usepwd)";
cm.Parameters.Add("@id",SqlDbType.Char,10);
cm.Parameters.Add("@username",SqlDbType.Char,10);
cm.Parameters.Add("@usepwd",SqlDbType.Char,10);
MessageBox.Show(dat.Rows[0][1].ToString());
cm.Parameters[0].Value=dat.Rows[0][0].ToString();
cm.Parameters[1].Value=dat.Rows[0][1].ToString();
cm.Parameters[2].Value=dat.Rows[0][2].ToString();
System.Data.SqlClient.SqlDataAdapter da=new SqlDataAdapter();
da.InsertCommand=cm;
conn.Open();
DataColumn[] keys = new DataColumn [1] ;
keys[0] = dat.Columns ["id"] ;
dat.PrimaryKey = keys ;
dat.AcceptChanges();
da.Update(dat);
MessageBox.Show(dat.Rows[0][1].ToString());
conn.Close();
}
注:dat 等相没有声明的在最前面声明过了;
大家帮看看,现在程序能运行。没错,可是就是不能更添加到数据库,大家看看应怎么改啊!!谢谢!!