using System;
using System.Data;
using System.Data.SqlClient;namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
……………
……………
//以上是连接数据库,为了简明说清我的问题,我省去了.
//设置Sql语句
SqlCommand MySqlCommand=MySqlConnection.CreateCommand();
MySqlCommand.CommandText="Execute AddProduct @MyProductID OutPut,@MyProductName,@MySupplierId ,@MyCategoryId,@MyQuantityPerUnit,@MyUnitPrice,@MyUnitsInStock,@MyUnitsOnOrder,@MyReorderLevel,@MyDiscontinued";
//添加存储过程的各个参数
MySqlCommand.Parameters.Add("@MyProductID",SqlDbType.Int);
MySqlCommand.Parameters["@MyProductID"].Direction=ParameterDirection.Output;//设置@MyProductID参数
MySqlCommand.Parameters.Add("@MyProductName",SqlDbType.NVarChar,40,ProductName).value="Wedgt";//设置@MyProductName参数
MySqlCommand.Parameters.Add("@MySupplierId",SqlDbType.Int,4,SupplierId).value=1;
MySqlCommand.Parameters.Add("@MyCategoryId",SqlDbType.Int,4,CategoryId).value=1;
MySqlCommand.Parameters.Add("@MyQuantityPerUnit",SqlDbType.NVarChar,20,QuantityPerUnit).value="1 per box";
MySqlCommand.Parameters.Add("@MyUnitPrice",SqlDbType.Money,8,UnitPrice).value=5.99; //单位价格
MySqlCommand.Parameters.Add("@MyUnitsInStock",SqlDbType.SmallInt,2,UnitsInStock).value=10;
MySqlCommand.Parameters.Add("@MyUnitsOnOrder",SqlDbType.SmallInt,2,UnitsOnOrder).value=5;
MySqlCommand.Parameters.Add("@MyReorderLevel",SqlDbType.SmallInt,2,ReorderLevel).value=5;
MySqlCommand.Parameters.Add ("@MyDiscontinued",SqlDbType.Bit,1,Discontinued).value=1;
//执行存储过程
MySqlCommand.ExecuteNonQuery();
//关闭数据库的连接
MySqlConnection.Close();
}
}
}运行程序时,出现的问题是:
名称“CategoryId”在类或命名空间“ConsoleApplication1.Class1”中不存在
名称“Discontinued”在类或命名空间“ConsoleApplication1.Class1”中不存在
名称“ProductName”在类或命名空间“ConsoleApplication1.Class1”中不存在
名称“QuantityPerUnit”在类或命名空间“ConsoleApplication1.Class1”中不存在
名称“ReorderLevel”在类或命名空间“ConsoleApplication1.Class1”中不存在
名称“SupplierId”在类或命名空间“ConsoleApplication1.Class1”中不存在
名称“UnitPrice”在类或命名空间“ConsoleApplication1.Class1”中不存在
名称“UnitsInStock”在类或命名空间“ConsoleApplication1.Class1”中不存在
名称“UnitsOnOrder”在类或命名空间“ConsoleApplication1.Class1”中不存在
对于增加参数的Add过程,我也是按照“参数名,数据类型,列宽,源列名”的顺序加入的呀,而且参数名跟数据库中存储过程里定义的参数名完全一样(大小写也一样),数据类型和列的长度跟表中的一样, 源列名跟表中的列名大小写也完全一样,而且存储过程与表中列的定义也一样。怎么还出现这样的错误提示呢?
using System.Data;
using System.Data.SqlClient;namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
……………
……………
//以上是连接数据库,为了简明说清我的问题,我省去了.
//设置Sql语句
SqlCommand MySqlCommand=MySqlConnection.CreateCommand();
MySqlCommand.CommandText="Execute AddProduct @MyProductID OutPut,@MyProductName,@MySupplierId ,@MyCategoryId,@MyQuantityPerUnit,@MyUnitPrice,@MyUnitsInStock,@MyUnitsOnOrder,@MyReorderLevel,@MyDiscontinued";
//添加存储过程的各个参数
MySqlCommand.Parameters.Add("@MyProductID",SqlDbType.Int);
MySqlCommand.Parameters["@MyProductID"].Direction=ParameterDirection.Output;//设置@MyProductID参数
MySqlCommand.Parameters.Add("@MyProductName",SqlDbType.NVarChar,40,ProductName).value="Wedgt";//设置@MyProductName参数
MySqlCommand.Parameters.Add("@MySupplierId",SqlDbType.Int,4,SupplierId).value=1;
MySqlCommand.Parameters.Add("@MyCategoryId",SqlDbType.Int,4,CategoryId).value=1;
MySqlCommand.Parameters.Add("@MyQuantityPerUnit",SqlDbType.NVarChar,20,QuantityPerUnit).value="1 per box";
MySqlCommand.Parameters.Add("@MyUnitPrice",SqlDbType.Money,8,UnitPrice).value=5.99; //单位价格
MySqlCommand.Parameters.Add("@MyUnitsInStock",SqlDbType.SmallInt,2,UnitsInStock).value=10;
MySqlCommand.Parameters.Add("@MyUnitsOnOrder",SqlDbType.SmallInt,2,UnitsOnOrder).value=5;
MySqlCommand.Parameters.Add("@MyReorderLevel",SqlDbType.SmallInt,2,ReorderLevel).value=5;
MySqlCommand.Parameters.Add ("@MyDiscontinued",SqlDbType.Bit,1,Discontinued).value=1;
//执行存储过程
MySqlCommand.ExecuteNonQuery();
//关闭数据库的连接
MySqlConnection.Close();
}
}
}运行程序时,出现的问题是:
名称“CategoryId”在类或命名空间“ConsoleApplication1.Class1”中不存在
名称“Discontinued”在类或命名空间“ConsoleApplication1.Class1”中不存在
名称“ProductName”在类或命名空间“ConsoleApplication1.Class1”中不存在
名称“QuantityPerUnit”在类或命名空间“ConsoleApplication1.Class1”中不存在
名称“ReorderLevel”在类或命名空间“ConsoleApplication1.Class1”中不存在
名称“SupplierId”在类或命名空间“ConsoleApplication1.Class1”中不存在
名称“UnitPrice”在类或命名空间“ConsoleApplication1.Class1”中不存在
名称“UnitsInStock”在类或命名空间“ConsoleApplication1.Class1”中不存在
名称“UnitsOnOrder”在类或命名空间“ConsoleApplication1.Class1”中不存在
对于增加参数的Add过程,我也是按照“参数名,数据类型,列宽,源列名”的顺序加入的呀,而且参数名跟数据库中存储过程里定义的参数名完全一样(大小写也一样),数据类型和列的长度跟表中的一样, 源列名跟表中的列名大小写也完全一样,而且存储过程与表中列的定义也一样。怎么还出现这样的错误提示呢?
解决方案 »
- 请教Winform程序RichTextBox控件只显示前1000行,如何做到,谢谢
- MVC 的理解?
- 字符串的对比取值
- 在DataGrid中显示数据库时间时总是2004-1-1 0:00:00 这种格式
- 先调用程序再调用WebService就没有问题,如果先调用WebService再调用程序就出问题
- c#读文件的问题,求大家指教
- 请教下C#中能否在图片上动态添加文字
- 上传文件来服务器,如何给服务器中的文件夹设置权限
- 如何调用"Internet选项"属性框?
- 没有实现IEnumerable接口怎么也有Getenumerator()?
- 关于C#中使用messagebox的问题(急求解答!)
- c#有限广播问题(255.255.255.255)
SqlCommand MySqlCommand=MySqlConnection.CreateCommand();
MySqlCommand.CommandText="Execute AddProduct @MyProductID OutPut,@MyProductName,@MySupplierId ,@MyCategoryId,@MyQuantityPerUnit,@MyUnitPrice,@MyUnitsInStock,@MyUnitsOnOrder,@MyReorderLevel,@MyDiscontinued";
//添加存储过程的各个参数
MySqlCommand.Parameters.Add("@MyProductID",SqlDbType.Int);
MySqlCommand.Parameters["@MyProductID"].Direction=ParameterDirection.Output;//设置@MyProductID参数
MySqlCommand.Parameters.Add("@MyProductName",SqlDbType.NVarChar,40,ProductName).value="Wedgt";//设置@MyProductName参数
MySqlCommand.Parameters.Add("@MySupplierId",SqlDbType.Int,4,SupplierId).value=1;
MySqlCommand.Parameters.Add("@MyCategoryId",SqlDbType.Int,4,CategoryId).value=1;
MySqlCommand.Parameters.Add("@MyQuantityPerUnit",SqlDbType.NVarChar,20,QuantityPerUnit).value="1 per box";
MySqlCommand.Parameters.Add("@MyUnitPrice",SqlDbType.Money,8,UnitPrice).value=5.99; //单位价格
MySqlCommand.Parameters.Add("@MyUnitsInStock",SqlDbType.SmallInt,2,UnitsInStock).value=10;
MySqlCommand.Parameters.Add("@MyUnitsOnOrder",SqlDbType.SmallInt,2,UnitsOnOrder).value=5;
MySqlCommand.Parameters.Add("@MyReorderLevel",SqlDbType.SmallInt,2,ReorderLevel).value=5;
MySqlCommand.Parameters.Add ("@MyDiscontinued",SqlDbType.Bit,1,Discontinued).value=1;————————————————————————————
这两块位置是不是要换换?
如
private string CategoryId = " CategoryId";
....
提示:“System.Data.SqlClient.SqlParameter”并不包含对“value”的定义即://定义参数
string SupplierId = "SupplierId";
string CategoryId = "CategoryId";
string QuantityPerUnit = "QuantityPerUnit";
string UnitPrice = "UnitPrice";
string UnitsInStock = "UnitsInStock";
string UnitsOnOrder = "UnitsOnOrder";
string ReorderLevel = "ReorderLevel";
string Discontinued = "Discontinued";
//连接到数据库,并保持连接状态。
string ConnectionString="data source=.;database=NewDb;Integrated Security=SSPI";
SqlConnection MySqlConnection=new SqlConnection(ConnectionString);
MySqlConnection.Open();
//设置Sql语句
SqlCommand MySqlCommand=MySqlConnection.CreateCommand();
………………
………………
这怎么办呢?
MySqlCommand.Parameters["@MyProductName"].Value="Wedgt";
…………//依照上面的格式设置各个参数
…………
//执行存储过程
MySqlCommand.ExecuteNonQuery();可是运行到MySqlCommand.ExecuteNonQuery();这句时,却提示:ExecuteNonQuery错误: 标识符“ExecuteNonQuery”超出范围怎么办呢?
然后再把该对象加到command的parameter的集合里
CategoryId在这里不是变量呀,是数据表中的列名呀.marginal(猪在天上飘):
"先实例化parameter对象
然后再把该对象加到command的parameter的集合里"我试过了,用这样的方式:
SqlParameter param1 = new SqlParameter("@MyProductName",SqlDbType.NVarChar,40);
param1.Value = "Wedgt";
MySqlCommand.Parameters.Add(param1);
………………
………………
提示同样的错误:ExecuteNonQuery错误: 标识符“ExecuteNonQuery”超出范围
再把参数加入到Parameter的集合里,然后设Command.CommandText = "你的存储过程名字"
然后运行Command.ExecuteNonQuery(); public override void exeNonProc(string procName, SqlParameter[] para)
{
Open();
cmd.Parameters.Clear();
cmd.CommandText = procName;
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
for(int i = 0;i<para.Length;i++)
{
cmd.Parameters.Add(para[i]);
}
cmd.ExecuteNonQuery();
Close();
}
这个是我原来用来执行存储过程的函数,其中,存储过程名和存储过程参数是由调用方传入的
具体情况如下:………
………
//以上是连接数据库和设置MySqlCommand
MySqlCommand.CommandType=CommandType.StoredProcedure;//指定类型是存储过程
MySqlCommand.CommandText="AddProduct"; //AddProduct是存储过程名
//添加存储过程的参数。
MySqlCommand.Parameters.Clear();
MySqlCommand.Parameters.Add("@MyProductID",SqlDbType.Int);
MySqlCommand.Parameters["@MyProductID"].Direction=ParameterDirection.Output;------①
MySqlCommand.Parameters.Add("@MyProductName",SqlDbType.NVarChar,40,"ProductName");//设置@MyProductName参数
MySqlCommand.Parameters["@MyProductName"].Value="Wedgt";----------②--------------------------------------运行到①句时提示:MyProductID 错误: 标识符“MyProductID”超出范围运行到②句时提示:MyProductName错误: 标识符“MyProductName”超出范围 这究竟该怎样解决呢?
我用的数据库名字是test,表是test_table,存储过程是add_record有两个output参数,下面是详细的sql语句:
create database test
go
create table test_table
(
id int primary key,
name varchar(50)
)
go
create proc add_record
@id int output,
@name varchar(50) output
as
begin
declare @temId int,@temName varchar(50)
if((select count(id) from test_table) = 0)
begin
select @temId = 1
end
else
begin
select @temId = (select max(id) from test_table) + 1
end
select @temName = 'new record' + CAST(@temId as varchar)
select @id = @temId
select @name = @temName
insert into test_table values(@temId,@temName)
end
go
////////////////////////////////////////////////////////////////////////
以下是c#代码,只有功能上的实现,try,catch没有写,lz在自己代码里可以完善:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace about_database
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btnAddRec_Click(object sender, EventArgs e)
{
string strCon = "server=.;database=test;user id=sa;pwd=sa";
SqlConnection con = new SqlConnection(strCon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "add_record";
cmd.CommandType = CommandType.StoredProcedure;
//添加参数
SqlParameter para1 = new SqlParameter("@id", SqlDbType.Int);
para1.Direction = ParameterDirection.Output;
SqlParameter para2 = new SqlParameter("@name", SqlDbType.VarChar, 50);
para2.Direction = ParameterDirection.Output;
cmd.Parameters.Add(para1);
cmd.Parameters.Add(para2);
//执行存储过程
con.Open();
cmd.ExecuteNonQuery();
con.Close();
//在listbox中显示返回参数的值
this.lsbConsole.Items.Add(para1.Value.ToString());
this.lsbConsole.Items.Add(para2.Value.ToString());
}
}
}