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

时间:2012-05-12 06:38:10

标签: c# sql-server-2005 sqlcommand

我想将SqlCommand结果转换为Int值我该怎么做? 请帮我解决下面的问题:

表中的stp_no具有设置标识属性。我想将自动生成的数字插入到另一个表中,但它显示总是错误,如“Connot将SqlCommand类型转换为Int类型”

    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解决此问题:)

2 个答案:

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

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