0

我的表中有一个复合键(col1,col2)我在col1 上
收到“无法将空值插入主键”错误,但实际上我没有尝试插入空值,我在调试模式下检查过,值不是没有
任何想法?

这是我的存储过程:

CREATE PROCEDURE [dbo].[proc1]
@col1 char(11) = NULL,
@col2 nchar(50) = NULL,
@col3 real = NULL
AS
BEGIN
SET NOCOUNT ON;

-- Insert statements for procedure here
update dbo.table1 set col2 = @col2 where col1 = @col1 AND col3 = @col3
if @@ROWCOUNT=0
insert into dbo.table1 ( col1, col2, col3 ) values ( @col1, @col2, @col3 )



这是我的代码:

SqlConnection conn = create_conn();
conn.Open();
SqlCommand command = new SqlCommand("another_proc", conn);
command.Parameters.Add("col1", SqlDbType.Char).Value = col1;
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);

command = new SqlCommand("proc1", conn);

// here i set the value of the parameter
**command.Parameters.Add("col1", SqlDbType.Char).Value = col1;**

command.Parameters.Add("col2", SqlDbType.NChar);
command.Parameters.Add("col3", SqlDbType.Real);
double y, yk, ym, yy;
int k, m;
string col2;
foreach (DataRow dr in ds.Tables[0].Rows)
{
    col2 = dr["col2"].ToString().Trim();
    yk = Double.Parse(dr["yk"].ToString());
    ym = Double.Parse(dr["ym"].ToString());
    yy = Double.Parse(dr["yy"].ToString());
    k = Int32.Parse(dr["k"].ToString());
    m = Int32.Parse(dr["m"].ToString());
    y = Double.Parse(dr["y"].ToString());
    double col3 = ...some calculation here...;
    command.Parameters["col2"].Value = col2;
    command.Parameters["col3"].Value = col3;
    command.ExecuteNonQuery();
}
conn.Close();



更新代码:

SqlConnection conn = create_conn();
conn.Open();
SqlCommand command = new SqlCommand("another_proc", conn);
command.Parameters.Add("col1", SqlDbType.Char).Value = col1;
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);

command.CommandText = "proc1";
command.Parameters.Add("col2", SqlDbType.NChar);
command.Parameters.Add("col3", SqlDbType.Real);
double y, yk, ym, yy;
int k, m;
string col2;
foreach (DataRow dr in ds.Tables[0].Rows)
{
    col2 = dr["col2"].ToString().Trim();
    yk = Double.Parse(dr["yk"].ToString());
    ym = Double.Parse(dr["ym"].ToString());
    yy = Double.Parse(dr["yy"].ToString());
    k = Int32.Parse(dr["k"].ToString());
    m = Int32.Parse(dr["m"].ToString());
    y = Double.Parse(dr["y"].ToString());
    double col3 = k * (yk / 100) + m * (ym / 100) + y * (yy / 100);
    command.Parameters["col2"].Value = col2;
    command.Parameters["col3"].Value = col3;
    command.ExecuteNonQuery();
}
conn.Close();
4

2 回答 2

2

Col1 和 col2 不应为空,因为它们正在生成复合键。改变

@col1 char(11) = NULL,
@col2 nchar(50) = NULL,

@col1 char(11) ,
@col2 nchar(50),

还将所有参数的参数添加更改为sql过程

command.Parameters.Add("col1", SqlDbType.Char).Value = col1;

command.Parameters.Add("@col1", SqlDbType.Char).Value = col1;

或者

 command.Parameters.Add("@col1", SqlDbType.Char);
 command.Parameters["@col1"].Value = col1;

编辑

错误“过程或函数 'proc1' 需要参数 '@col1',但未提供。” 你得到的是误导,问题是由于没有设置CommandType。在执行命令之前创建新命令并添加以下语句。

SqlCommand command = new SqlCommand("proc1", conn);
command1.CommandType = CommandType.StoredProcedure;

还要清除循环中的参数集合。

for (int i = 0; i < 10; i++)
{
   col1 = "Col1 value " + i;
   col2 = "Col2 value " + i;
   col3 = "Col3 value" + i;

   command.Parameters.Clear();

   command.Parameters.Add("@col1", SqlDbType.VarChar).Value = col1;
   command.Parameters.Add("@col2", SqlDbType.VarChar).Value = col2;
   command.Parameters.Add("@col3", SqlDbType.VarChar).Value = col3;

   command1.ExecuteNonQuery();
} 
于 2012-06-21T10:19:51.700 回答
2

我们怎么都错过了?

command.CommandType = CommandType.StoredProcedure;
于 2012-06-21T19:45:42.943 回答