尝试插入非空值时“无法将空值插入主键”错误

时间:2012-06-21 10:03:09

标签: c# exception-handling ado.net null primary-key

我的表格中有一个复合键( 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();

2 个答案:

答案 0 :(得分: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();
} 

答案 1 :(得分:2)

我们怎么都错过了?

command.CommandType = CommandType.StoredProcedure;