0

这是我正在构建的一个例程,用于处理 SQL Server 中的 5 个表的插入和选择。Select 语句返回具有行数的数据表,而 Insert 则不返回。我相信问题在于 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;

}
4

1 回答 1

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); 
于 2013-03-27T21:54:44.713 回答