作者:Max_coffee | 来源:互联网 | 2023-10-12 09:26
我现在在用SQLClient模式写一个程序目标是假如我在一个TextBox里面写入一个信息然后按提交按钮就把信息插入到数据库中我要整个程序我写了一半但没用我等着
我现在在用SQLClient模式写一个程序
目标是 假如我在一个TextBox里面写入一个信息 然后按提交按钮 就把信息插入到数据库中
我要整个程序
我写了一半
但没用
我等着
11 个解决方案
string strSql = "insert into t_tablename values ('" + TextBox1.Text + "','" + TextBox2.Text +"'')"
然后执行这条sql语句
用事务呀,
Cn.Open();
SqlTransaction trans=Cn.BeginTransaction();
SqlCommand Cm=new SqlCommand();
Cm.COnnection=Cn;
Cm.Transaction()=trans;
try
{
Cm.CommandText="insert ...."; //Sql插入命令
Cm.ExecuteNonquery();
trans.Commit();
}
catch(Exception xcp)
{
trans.Rollback();
}
finally
{
Cn.Close();
}
SqlConnection cOnn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
//insert sql
SqlCommand comm=new SqlCommand("insert into testgrid (UserName,Country,State,Enabled) values(@UserName,@province,@city,@Enabled)",conn);
//update sql
//SqlCommand comm=new SqlCommand("update testgrid set UserName=@UserName,Country=@province,State=@city,Enabled=@Enabled where UserID=@UserID",conn);
SqlParameter parm1=new SqlParameter("@UserName",SqlDbType.NVarChar,50);
parm1.Value=TextBox1.Text;
SqlParameter parm2=new SqlParameter("@province",SqlDbType.NVarChar,50);
parm2.Value=TextBox1.Text;
SqlParameter parm3=new SqlParameter("@city",SqlDbType.NVarChar,50);
parm3.Value=TextBox1.Text;
SqlParameter parm4=new SqlParameter("@Enabled",SqlDbType.NVarChar,50);
parm4.Value=TextBox1.Text;
SqlParameter parm5=new SqlParameter("@UserID",SqlDbType.Int);
parm5.Value==TextBox1.Text;
comm.Parameters.Add(parm1);
comm.Parameters.Add(parm2);
comm.Parameters.Add(parm3);
comm.Parameters.Add(parm4);
comm.Parameters.Add(parm5);
conn.Open();
comm.ExecuteNonQuery();
我的代码是这样的但总报错
private void button1_Click(object sender, System.EventArgs e)
{
SqlConnection Connection;
SqlCommand Command;
// 连接
string strCOnnection="server=92D51A42FB9E478;user id=sa;password =; Connect Timeout=30;initial catalog=fighting;";
//创建连接
Connection = new SqlConnection(strConnection);
//打开连接
Connection.Open();
//创建命令
Command=new SqlCommand();
Command.Connection = Connection;
//设置命令SQL语句
Command.CommandText = "insert into fangfei(ff_id,ff_name,ff_ffk,ff_date,ff_money,ff_zhibiaoren,ff_bmid) values('" +this.textBox1.Text +"','"+this.textBox2.Text+"','"+this.comboBox1.Text+"','"+this.textBox4.Text+"',"+this.textBox3.Text+",'"+this.textBox5.Text+"','"+this.textBox6.Text+"')";
//执行
int query = Command.ExecuteNonQuery();
if(query==1)
{
Console.Write("执行成功");
}
else
{
Console.Write("执行失败");
}
}
报错:“fighting.exe”: 已加载“c:\windows\assembly\gac\system.xml\1.0.5000.0__b77a5c561934e089\system.xml.dll”,未加载符号。
未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。
其他信息: 系统错误。
程序“[3188] fighting.exe”已退出,返回值为 0 (0x0)。
你的是控制台程序还是Web应用程序,还是Windows程序?