在C#语言中怎么写插入数据库代码

lijian910wolf 2005-11-23 09:37:29
我现在在用SQLClient模式写一个程序
目标是 假如我在一个TextBox里面写入一个信息 然后按提交按钮 就把信息插入到数据库中
我要整个程序
我写了一半
但没用


我等着
...全文
979 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
pikapi 2006-05-10
  • 打赏
  • 举报
回复
问大家一个极菜的问题,那个@的标识是怎么用的?
yaye_85 2005-11-24
  • 打赏
  • 举报
回复
你有没有对sql命名空间啊
lijian910wolf 2005-11-24
  • 打赏
  • 举报
回复
你还在吗
帮我看看咯
lijian910wolf 2005-11-24
  • 打赏
  • 举报
回复
Windows程序
jxufewbt 2005-11-24
  • 打赏
  • 举报
回复
你的是控制台程序还是Web应用程序,还是Windows程序?
lijian910wolf 2005-11-24
  • 打赏
  • 举报
回复
我的代码是这样的但总报错
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)。
fx_blog 2005-11-23
  • 打赏
  • 举报
回复
呵呵~!~!~
singlepine 2005-11-23
  • 打赏
  • 举报
回复
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();
zeusvenus 2005-11-23
  • 打赏
  • 举报
回复
可以借鉴sqlhelper.cs类的实现方法
bufan2162 2005-11-23
  • 打赏
  • 举报
回复
用事务呀,
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();
}
i_love_sc 2005-11-23
  • 打赏
  • 举报
回复
string strSql = "insert into t_tablename values ('" + TextBox1.Text + "','" + TextBox2.Text +"'')"
然后执行这条sql语句

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧