菜鸟请问:c#中怎样写这条select 语句??

来源:百度知道 编辑:UC知道 时间:2024/06/22 17:06:00
有一个表table1,其中一列是id,id是整数型,现在
我想在c#中编写语句,从table1中读出id=i的记录,语句如下:

int i=100;
string sql="select * from table1 where id=" + i;
请问这样写对吗?我总觉得好像不对劲,是不是应该改成:

string sql="select * from table1 where id=" + i.ToString();

我困惑的是:我的sql中没有出现数字,只有i,那么100这个数字是如何(在查询的时候)起作用的?

select 后面少了 *

如果数据中字段 id 是数字型的话
string sql="select * from table1 where id=" + i.ToString();

如果不是数字型 要加单引号
string sql="select * from table1 where id='" + i.ToString()+"'";

string sql ="INSERT OrderBook (UnitPrice) VALUES (@UnitPrice)";

SqlParameter[] para = new SqlParameter[]
{
new SqlParameter("@UnitPrice", order.UnitPrice)
};
DBHelper.ExecuteCommand(sql, para);

实在行不通 考虑一下这样的写法吧
string sql="select * from table1 where id=" + i.ToString();
这个能实现吧

在这种情况下,ToString()是自动被调用的。所以不写也可以。

如果你那个id是int类型的就不需要加ToString()

string sql=string.format("select * from table1 where id={0}",i);