Lufer

  • 首页
  • 编程
  • 学习笔记
  • 日常折腾
Lufer
Code the World
  1. 首页
  2. 编程
  3. .NET
  4. 正文

C Sharp开发手记--通过传参的方式连接数据库

2016年5月31日 411点热度 0人点赞 0条评论

通过传参的方式与数据库进行连接,可以很好的解决字符串过长,字符串中包含或等问题

//构建SQL连接
SqlConnection myconnection = new SqlConnection("//ur sql connection string here");
myconnection.Open();

//建立参数方式的SQL语句
String sqlcommand = "insert into TABLE_NAME values(@id,@name.//@somethingelse";
SqlCommand mycommand = new SqlCommand(sqlcommand, myconnection);

//设定参数格式
mycommand.Parameters.Add(new SqlParameter("@id", System.Data.SqlDbType.Int, 0));
mycommand.Parameters.Add(new SqlParameter("@name", System.Data.SqlDbType.VarChar, 20));
//mycommand.Parameters.Add(new SqlParameter("@somethingelse", OtherType, Length));


//向参数中填值
 mycommand.Parameters\["@id"\].Value = _id;
mycommand.Parameters\["@name"\].Value = _name;
//mycommand.Parameters\["somethingelse"\].Value = _somethingelse;

//执行语句
mycommand.ExecuteNonQuery();
标签: .NET
最后更新:2023年7月10日

Lufer

新的一天开始啦

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2022 lufer.cc.

Theme Kratos Made By Seaton Jiang

鲁ICP备2021045819号