0

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

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

    SqlCommand dvgcmd, snocmd;
    snocmd = new SqlCommand("SELECT stp_no FROM MaterialTestMaster", con);
    dvgcmd = new SqlCommand("INSERT INTO MaterialTestDetail(stp_no,test_no,test_name,test_type,test_spec,high_limit,low_limit)" +
                                                     "VALUES('"+ snocmd +"','" + @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);

请帮助 M 解决这个问题 :)

4

2 回答 2

0
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);
于 2012-05-12T06:44:05.793 回答
0

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);
于 2012-05-12T06:50:56.910 回答