从查询到dataTable获取SCOPE_IDENTITY()的问题

时间:2013-03-27 20:52:21

标签: asp.net sql-server c#-3.0

这是我正在构建的例程,用于处理SQL Server中5个表的插入和选择。 Select语句返回一个带行数的数据表,Inserts不返回。我相信问题出在SQLAdapter和DataTable之间的绑定中。如何从插入到数据表中的行中获取SCOPE_IDENTITY?我知道我可以把它作为一个int,但是代码的主体需要从中返回数据表。

    public DataTable dbQuery(string queryType, modelsClass model)
{
    string query = null;
    char subQueryType = 'd';
    SqlConnection connectionString = new SqlConnection(ConfigurationManager.ConnectionStrings["inventory_v2ConnectionString"].ConnectionString);
    SqlCommand queryCommand = new SqlCommand(@query, connectionString);
    SqlDataAdapter sqlAdapt = new SqlDataAdapter();
    sqlAdapt.Dispose();
    DataTable dt = new DataTable("return");

    try
    {

        switch(queryType)
        {
            case "OEMSelect":
                {

                    query = "SELECT [ID] FROM [Models_OEMs] WHERE Manufacturer = @OEM";
                    subQueryType = 'S';
                    queryCommand.Parameters.Clear();
                    queryCommand.Parameters.AddWithValue("@OEM",model.OEM);

                    break;
                }
            case "PriSelect":
                {
                    query = "SELECT [ID]FROM [Models_PriModels] WHERE PriModel = @PriModel";
                    subQueryType = 'S';
                    queryCommand.Parameters.Clear();
                    queryCommand.Parameters.AddWithValue("@PriModel", model.PriModel);
                    break;
                }
            case "SecSelect":
                {
                    query = "SELECT [ID] FROM [Models_SecModels] WHERE SecModel = @SecModel";
                    subQueryType = 'S';
                    queryCommand.Parameters.Clear();
                    queryCommand.Parameters.AddWithValue("@SecModel", model.SecModel);
                    break;
                }
            case "FormSelect":
                {
                    query = "SELECT [ID] FROM [Models_FormFactor] WHERE Form = @Form";
                    subQueryType = 'S';
                    queryCommand.Parameters.Clear();
                    queryCommand.Parameters.AddWithValue("@Form", model.FormFactor);
                    break;
                }
            case "ModelSelect":
                {
                    //Does Model Already exist?
                    query = "SELECT [ID] FROM [Models_OEMs] WHERE Manufacturer = @Manufacturer AND PriModel = @PriModelID AND SecModel  = @SecModelID AND FormFactor = @FormFactorID";
                    subQueryType = 'S';
                    queryCommand.Parameters.Clear();
                    queryCommand.Parameters.AddWithValue("@Manufacturer", model.OEMID);
                    queryCommand.Parameters.AddWithValue("@PriModel", model.PriModelID);
                    queryCommand.Parameters.AddWithValue("@SecModel", model.SecModelID);
                    queryCommand.Parameters.AddWithValue("@FormFactor", model.FormFactorID);
                    break;
                }
            case "OEMInsert":
                {
                    query = "INSERT INTO Models_OEMs (Manufacturer) VALUES (@OEM); SELECT SCOPE_IDENTITY();";
                    subQueryType = 'I';
                    queryCommand.Parameters.Clear();
                    queryCommand.Parameters.AddWithValue("@OEM", model.OEM);

                    break;
                }
            case "PriInsert":
                {
                    query = "INSERT INTO Models_PriModels (PriModel) VALUES (@PriModel); SELECT SCOPE_IDENTITY();";
                    subQueryType = 'I';
                    queryCommand.Parameters.Clear();
                    queryCommand.Parameters.AddWithValue("@PriModel", model.PriModel);
                    break;
                }
            case "SecInsert":
                {
                    query = "INSERT INTO Models_SecModel (SecModel) VALUES (@SecModel); SELECT SCOPE_IDENTITY();";
                    subQueryType = 'I';
                    queryCommand.Parameters.Clear();
                    queryCommand.Parameters.AddWithValue("@SecModel", model.SecModel);
                    break;
                }
            case "FormInsert":
                {
                    query = "INSERT INTO Models_FormFactor (Form) VALUES (@FormFactor); SELECT SCOPE_IDENTITY();";
                    subQueryType = 'I';
                    queryCommand.Parameters.Clear();
                    queryCommand.Parameters.AddWithValue("@FormFactor", model.FormFactor);
                    break;
                }
            case "ModelInsert":
                {
                    query = "INSERT INTO Models (Manufacturer, PriModel, SecModel, FormFactor, Active) VALUES (@Manufacturer, @PriModel, @SecModel, @FormFactor, 'Y'); SELECT SCOPE_IDENTITY();";
                    subQueryType = 'I';
                    queryCommand.Parameters.Clear();
                    queryCommand.Parameters.AddWithValue("@Manufacturer", model.OEMID);
                    queryCommand.Parameters.AddWithValue("@PriModel", model.PriModelID);
                    queryCommand.Parameters.AddWithValue("@SecModel", model.SecModelID);
                    queryCommand.Parameters.AddWithValue("@FormFactor", model.FormFactorID);

                    break;
                }
            default:
                {
                    subQueryType = 'D';  //Do nothing. The program should never get here.
                    break;
                }

        }

        queryCommand.CommandText = query;


        switch (subQueryType)
        {
            case 'S':
                {
                    sqlAdapt.SelectCommand = queryCommand;
                    connectionString.Open();
                    queryCommand.ExecuteNonQuery();
                    sqlAdapt.Fill(dt);
                    queryCommand.Dispose();
                    connectionString.Close();
                    break;
                }
            case 'I':
                {

                    sqlAdapt.InsertCommand = queryCommand;
                    connectionString.Open();
                    try
                    {
                        DataColumn dc1 = new DataColumn();
                        dc1.DataType = typeof(String);
                        dc1.ColumnName = "ReturnedID";
                        dt.Columns.Add(dc1);
                        DataRow dr = dt.NewRow();
                        dr["ReturnedID"] = queryCommand.ExecuteScalar();
                        dt.Rows.Add(dr);                          
                    }
                    catch(Exception ex)
                    {
                        DEBUG.Text = DEBUG.Text + " Insert failed! Error: " + ex.Message;
                    }
                    queryCommand.Dispose();
                    connectionString.Close();                         
                    break;
                }
            default:
                {
                    break;
                }
        }

        return dt;
    }

    catch (IOException ex)
    {
        DEBUG.Text += "Oops! I errored. : " + ex.Message;
    }
    return dt;

}

1 个答案:

答案 0 :(得分:0)

感谢@Melanie跳起我的大脑...... 我通过替换

解决了这个问题
queryCommand.ExecuteScalar(); 

使用:

DataColumn dc1 = new DataColumn();
dc1.DataType = typeof(String);
dc1.ColumnName = "ReturnedID";
dt.Columns.Add(dc1);
DataRow dr = dt.NewRow();
dr["ReturnedID"] = queryCommand.ExecuteScalar();
dt.Rows.Add(dr);