现在的位置: 首页 > 综合 > 正文

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

2014年11月22日 ⁄ 综合 ⁄ 共 358字 ⁄ 字号 评论关闭

在对数据库执行查询、更新、修改的时候不可避免的要使用参数或者变量,,而此时如果我们使用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);

抱歉!评论已关闭.