书上说Command提供程序不支持命令参数,必须使用@占位符,例如:
Command1.CommandText = "select sno, sname, sage FROM student WHERE sdept =@sdept"可是书上的例子:
if(combox1.Text == "计算机系")
command2.CommandText = "select sno, sname, sage FROM student WHERE (sdept = '计算机系')";这个例子又没有使用占位符.....我有点糊涂了
如果可以不用占位符是不是又可以写成
string sql ="select sno, sname, sage FROM student WHERE sdept = '+sdept+';
是存储过程必须用@,只要用@这个的就是存储过程是么?
老师说用存储过程来做这个DataGridView,那么怎样的才叫存储过程?
Command1.CommandText = "select sno, sname, sage FROM student WHERE sdept =@sdept"可是书上的例子:
if(combox1.Text == "计算机系")
command2.CommandText = "select sno, sname, sage FROM student WHERE (sdept = '计算机系')";这个例子又没有使用占位符.....我有点糊涂了
如果可以不用占位符是不是又可以写成
string sql ="select sno, sname, sage FROM student WHERE sdept = '+sdept+';
是存储过程必须用@,只要用@这个的就是存储过程是么?
老师说用存储过程来做这个DataGridView,那么怎样的才叫存储过程?
解决方案 »
- reportviewer打印datagridview中的数据
- listbox如何遍历所有记录写入数据库?
- 怎样让它干?
- WinForm MDI里打开一个子窗口然后最大化,再打开其他子窗口如何保持原样?
- WinForm 里面 怎么获取 TextBox的行数 和 字符串长度、
- 主窗口和非模态对话框之间实现拖拽
- 请用过NTKO OFFICE控件的同志来看看!!读取OLE的问题
- XML配置中文字符后添加中文后无法反序列化
- webform中dataGrid的“选择”等按钮怎么不起作用?
- using System.Runtime.Remoting.Channels.Tcp怎么引用不了?
- 求教 关于ASP.NET中使用<ol>标签自动编号的问题
- 求一条Sql语句
我也是新手
string sql ="select sno, sname, sage FROM student WHERE sdept = '+sdept+';
我都是么写的
存储过程在数据库里写
没写的
@变量
不用参数时,可以这样写
string sql ="select sno, sname, sage FROM student WHERE sdept = '"+sdept+"'";//sdept是文本类型,没有用到参数 可以直接写 不用@占位符你现在完全可以把存储过程理解成带参数的sql语句(但这是不对的),目前这样理解,以后就会知道Command1.CommandText = "select sno, sname, sage FROM student WHERE sdept =@sdept"
Command1.Parameters.Add(new SqlParameter("@sdept",参数值)); // 给参数@sdept赋值
string sql="select * from Tb where username='"+username+"'"
SqlParameter表示SqlCommand的参数
SqlParameter类型的数组作为SqlCommand的参数存在,配合转义字符@
Command1.CommandText = "select sno, sname, sage FROM student WHERE sdept =@sdept"SqlParameter表示SqlCommand的参数
SqlParameter类型的数组作为SqlCommand的参数存在,配合转义字符@也就是用到SqlParameter时,必须使用@占位符,是这个意思吧
用@作为占位符的 一般使用参数 ..........这个参数指:@sdept,还是指:SqlParameter表示SqlCommand的参数
存储过程理解成带参数的sql语句...有@符号的sql语句就是存储过程...这样理解么?
这样不行?if(combox1.Text == "计算机系")
command2.CommandText = "select sno, sname, sage FROM student WHERE (sdept = '计算机这样可以。
你自己看看吧?
http://www.google.com.hk/search?q=DbParameter+SqlParameter&hl=zh-CN&newwindow=1&safe=strict&rls=com.microsoft:zh-cn:IE-SearchBox&prmd=df&source=lnt&tbs=lr:lang_1zh-CN%7Clang_1zh-TW&lr=lang_zh-CN%7Clang_zh-TW&sa=X&ei=aeRKTKjqKIe6cY73seUM&ved=0CAcQpwU自己解决这个问题,别仅仅看一本书(容易把眼睛看坏)。
[color=#0000FF]Command1.CommandText = "select sno, sname, sage FROM student WHERE sdept =@sdept"
这个命令参数是什么?在C#中存储过程叫什么?什么意思?怎么才能知道它是存储过程?[/color]
2.存储过程是在数据库里面编译好的一段语句
例如
/// <summary>
/// 获取某商品信息
/// </summary>
/// <param name="theOrgCode">店号</param>
/// <param name="thePluCode">商品编号</param> public static DataSet GetPluProject(string theOrgCode, string thePluCode)
{
SqlParameter[] sortPara = { new SqlParameter("@PluCode", thePluCode),
new SqlParameter("@OrgCode", theOrgCode)
};
return SqlHelper.ExecuteDataset(SqlHelper.SQLCONN_CosSysDb, CommandType.StoredProcedure, " GetPluProject", sortPara);
}
REATE PROCEDURE [dbo].[yjGetPluProject]
(
@OrgCode char(10),
@PluCode char(10)
)
AS
Select Goods.PluCode,
PluName,
IsNull(BarCode, '') as BarCode,
GCount,
CCount,
AskCnt,
InitCount,
Spec,
IsNull(CargoNo, '') as CargoNo,
Unit,
Goods.JPrice,
Goods.NetJPrice,
JTaxRate,
Goods.Price,
DepCode,
Goods.Clscode,
VendorCode,
IsNull(Goods.PsPrice,Goods.JPrice) as PsPrice
From Goods, GClass C, GoodsOrg
where Goods.PluCode = GoodsOrg.PluCode
and GoodsOrg.OrgCode = @OrgCode
and Goods.ClsCode = C.ClsCode
and PluType = '0'
and (PluStatus = '0' or PluStatus = '' or PluStatus = '1')
and exists (select PluCode
from GoodsOrg
where PluCode = Goods.PluCode
and OrgCode = @OrgCode
and IsAsk = '1')
and Goods.Plucode = @PluCode
GO
REATE PROCEDURE [dbo].[yjGetPluProject]
(
@OrgCode char(10),
@PluCode char(10)
)
AS
Select Goods.PluCode,
PluName,
IsNull(BarCode, '') as BarCode,
GCount,
CCount,
AskCnt,
InitCount,
Spec,
IsNull(CargoNo, '') as CargoNo,
Unit,
Goods.JPrice,
Goods.NetJPrice,
JTaxRate,
Goods.Price,
DepCode,
Goods.Clscode,
VendorCode,
IsNull(Goods.PsPrice,Goods.JPrice) as PsPrice
From Goods, GClass C, GoodsOrg
where Goods.PluCode = GoodsOrg.PluCode
and GoodsOrg.OrgCode = @OrgCode
and Goods.ClsCode = C.ClsCode
and PluType = '0'
and (PluStatus = '0' or PluStatus = '' or PluStatus = '1')
and exists (select PluCode
from GoodsOrg
where PluCode = Goods.PluCode
and OrgCode = @OrgCode
and IsAsk = '1')
and Goods.Plucode = @PluCode
GO--存储过程如下
REATE PROCEDURE [dbo].[GetPluProject]
(
@OrgCode char(10),
@PluCode char(10)
)
AS
Select Goods.PluCode,
PluName,
IsNull(BarCode, '') as BarCode,
GCount,
CCount,
AskCnt,
InitCount,
Spec,
IsNull(CargoNo, '') as CargoNo,
Unit,
Goods.JPrice,
Goods.NetJPrice,
JTaxRate,
Goods.Price,
DepCode,
Goods.Clscode,
VendorCode,
IsNull(Goods.PsPrice,Goods.JPrice) as PsPrice
From Goods, GClass C, GoodsOrg
where Goods.PluCode = GoodsOrg.PluCode
and GoodsOrg.OrgCode = @OrgCode
and Goods.ClsCode = C.ClsCode
and PluType = '0'
and exists (select PluCode
from GoodsOrg
where PluCode = Goods.PluCode
and OrgCode = @OrgCode
and IsAsk = '1')
and Goods.Plucode = @PluCode
GO