C# 如何将SqlCommand类型转换为Int类型

C# 如何将SqlCommand类型转换为Int类型,c#,sql-server-2005,sqlcommand,C#,Sql Server 2005,Sqlcommand,我想将SqlCommand结果转换为Int值,我该怎么做?? 请帮助我,我尝试的内容如下: 表中的stp_no已设置标识属性。我想将自动生成的数字插入到另一个表中,但它总是显示错误,如“connt Convert SqlCommand Type To Int Type” 请帮助M解决此问题:)juergen的答案应该有效,您必须先执行命令,然后将结果用作下一步: int id = int.Parse(snocmd.ExecuteScalar().ToString()); dvgcmd = new

我想将SqlCommand结果转换为Int值,我该怎么做?? 请帮助我,我尝试的内容如下:

表中的stp_no已设置标识属性。我想将自动生成的数字插入到另一个表中,但它总是显示错误,如“connt Convert SqlCommand Type To Int Type”


请帮助M解决此问题:)

juergen的答案应该有效,您必须先执行命令,然后将结果用作下一步:

int id = int.Parse(snocmd.ExecuteScalar().ToString());
dvgcmd = new SqlCommand("INSERT INTO MaterialTestDetail(stp_no,test_no,test_name,test_type,test_spec,high_limit,low_limit)" +
                                                 "VALUES('"+ id +"','" + @matTstDataGridView.Rows[j].Cells[0].Value + "'," +
                                                         " '" + @matTstDataGridView.Rows[j].Cells[1].Value + "'," +
                                                         " '" + @matTstDataGridView.Rows[j].Cells[2].Value + "'," +
                                                         " '" + @matTstDataGridView.Rows[j].Cells[3].Value + "'," + 
                                                         " '" + @matTstDataGridView.Rows[j].Cells[4].Value + "'," +
                                                         " '" + @matTstDataGridView.Rows[j].Cells[5].Value + "')", con);

它给出错误“上下文中不允许子查询。允许使用Ony Scaler表达式”。ExecuteScaler()将仅插入第一条记录,因此当我尝试插入另一条记录时,它将给出主键约束错误,因为MaterialTestDetail hve stp_no是主键。您可以将命令更改为:snocmd=newsqlcommand(“从MaterialTestMaster中选择MAX(stp_no)”,con);但是,如果没有为每个要插入的MaterialTestDetail插入至少一个MaterialMaster,则可能会出现相同的问题。
SqlCommand dvgcmd;
dvgcmd = new SqlCommand("INSERT INTO MaterialTestDetail(stp_no,test_no,test_name,test_type,test_spec,high_limit,low_limit)" +
                                                 "VALUES((SELECT stp_no FROM MaterialTestMaster),'" + @matTstDataGridView.Rows[j].Cells[0].Value + "'," +
                                                         " '" + @matTstDataGridView.Rows[j].Cells[1].Value + "'," +
                                                         " '" + @matTstDataGridView.Rows[j].Cells[2].Value + "'," +
                                                         " '" + @matTstDataGridView.Rows[j].Cells[3].Value + "'," + 
                                                         " '" + @matTstDataGridView.Rows[j].Cells[4].Value + "'," +
                                                         " '" + @matTstDataGridView.Rows[j].Cells[5].Value + "')", con);
int id = int.Parse(snocmd.ExecuteScalar().ToString());
dvgcmd = new SqlCommand("INSERT INTO MaterialTestDetail(stp_no,test_no,test_name,test_type,test_spec,high_limit,low_limit)" +
                                                 "VALUES('"+ id +"','" + @matTstDataGridView.Rows[j].Cells[0].Value + "'," +
                                                         " '" + @matTstDataGridView.Rows[j].Cells[1].Value + "'," +
                                                         " '" + @matTstDataGridView.Rows[j].Cells[2].Value + "'," +
                                                         " '" + @matTstDataGridView.Rows[j].Cells[3].Value + "'," + 
                                                         " '" + @matTstDataGridView.Rows[j].Cells[4].Value + "'," +
                                                         " '" + @matTstDataGridView.Rows[j].Cells[5].Value + "')", con);