数据库操作语句的简单写法,用format方法

来源:互联网 发布:单片机价格 编辑:程序博客网 时间:2024/05/01 21:18

在对数据库执行查询、更新、修改的时候不可避免的要使用参数或者变量,,而此时如果我们使用string的Format方法将极大的减少代码和避免错误的发生。

string.Format方法是对一个字符串进行格式化,返回的也是字符串。如:

1。string str = string.Format("select * from 表 where username='{0}',txtName.Text");

2。string selStr = string.Format("select * from 表 where id={0}",整数);

3。string insertStr = string.Format("insert into 表(username,userpass) values('{0}','{1}')",txtName.Text,txtPass.Text);

原创粉丝点击