我是毕业生,碰到个问题请高手解决
在MySql数据库中,我有一个表kfa,它有3个属性id,name,password.
在C#中想把 textbox1.text,textbox2.text,texbox3.text的值传送给MYSQL数据库的kfa表中
引用using MySQLDriverCS之后,,自己不知道如何获取textbox的值并且传入数据库中,高手帮忙啊。
MySQLConnection conn = null;
conn = new MySQLConnection(new MySQLConnectionString("127.0.0.1", "my", "root", "test").AsString);
conn.Open();
//MySQLCommand commn = new MySQLCommand("set names gb2312", conn); -----这个听说是为了防止乱码产生,能起作用么?
//commn.ExecuteNonQuery();
string sql = "INSERT INTO `kfa` (`id`,`name`,`password`) VALUES ('textbox1.text','textbox2','textbox3')";
MySQLDataAdapter mda = new MySQLDataAdapter(sql, conn);
DataSet ds = new DataSet();
mda.Fill(ds, "table1");
this.Repeater1.DataSource = ds;
this.Repeater1.DataBind();
con.Close();
为什么一直抱错啊,我哭了,谁能告诉我啊.我油箱是[email protected]
求人了.
在MySql数据库中,我有一个表kfa,它有3个属性id,name,password.
在C#中想把 textbox1.text,textbox2.text,texbox3.text的值传送给MYSQL数据库的kfa表中
引用using MySQLDriverCS之后,,自己不知道如何获取textbox的值并且传入数据库中,高手帮忙啊。
MySQLConnection conn = null;
conn = new MySQLConnection(new MySQLConnectionString("127.0.0.1", "my", "root", "test").AsString);
conn.Open();
//MySQLCommand commn = new MySQLCommand("set names gb2312", conn); -----这个听说是为了防止乱码产生,能起作用么?
//commn.ExecuteNonQuery();
string sql = "INSERT INTO `kfa` (`id`,`name`,`password`) VALUES ('textbox1.text','textbox2','textbox3')";
MySQLDataAdapter mda = new MySQLDataAdapter(sql, conn);
DataSet ds = new DataSet();
mda.Fill(ds, "table1");
this.Repeater1.DataSource = ds;
this.Repeater1.DataBind();
con.Close();
为什么一直抱错啊,我哭了,谁能告诉我啊.我油箱是[email protected]
求人了.
int t1=this.textBox1.Text;
string t2=this.textBox2.Text;
string t3=this.textBox3.Text;
string sql=string.format("insert into kfa(id,name,password) values('{0}',''{1}'',''{2}'')",t1,t2,t3);
try{
connection.Open();
SqlCommand command=new SqlCommand(sql,connection);
count=(int)command.ExecuteScalar();
if(count==1)
MessageBox.Show("数据插入成功");
else
MessageBox.Show("数据插入失败");
}catch(SqlException se){
Console.WriteLine(se.Message);
}finally{
connection.Close();
}这个connection怎么回事应该清楚吧,这样应该可以插入了
祝你好运!