0

私はインターネット上のページやページを何日も調べて、さまざまなアプローチを試みてきましたが、これをどのように行うべきかまだわかりません.

3 回目の InsertCommand で、他の 2 つのテーブルの列を参照したいと思います。

// Populate a DataSet from multiple Tables... Works fine
sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = new SqlCommand("SELECT * FROM hardware", sqlConn);
sqlDA.Fill(ds, "Hardware");
sqlDA.SelectCommand.CommandText = "SELECT * FROM software";
sqlDA.Fill(ds, "Software");
sqlDA.SelectCommand.CommandText = "SELECT * FROM join_hardware_software";
sqlDA.Fill(ds, "HS Join");

// After DataSet has been changed, perform an Insert on relevant tables...
updatedDs = ds.GetChanges();
SqlCommand DAInsertCommand = new SqlCommand();
DAInsertCommand.CommandText = "INSERT INTO hardware (host, model, serial) VALUES (@host, @model, @serial)";
DAInsertCommand.Parameters.AddWithValue("@host", null).SourceColumn = "host";
DAInsertCommand.Parameters.AddWithValue("@model", null).SourceColumn = "model";
DAInsertCommand.Parameters.AddWithValue("@serial", null).SourceColumn = "serial";
sqlDA.InsertCommand = DAInsertCommand;
sqlDA.Update(updatedDs, "Hardware"); // Works Fine

DAInsertCommand.Parameters.Clear(); // Clear parameters set above
DAInsertCommand.CommandText = "INSERT INTO software (description) VALUES (@software)";
DAInsertCommand.Parameters.AddWithValue("@software", null).SourceColumn = "description";
sqlDA.InsertCommand = DAInsertCommand;
sqlDA.Update(updatedDs, "Software"); // Works Fine

DAInsertCommand.Parameters.Clear(); // Clear parameters set above
DAInsertCommand.CommandText = "INSERT INTO join_hardware_software (hardware_id, software_id) VALUES (@hardware_id, @software_id)";
// *****
DAInsertCommand.Parameters.AddWithValue("@hardware_id", null).SourceColumn = "?"; // I want to set this to be set to my 'hardware' table to the 'id' column.
DAInsertCommand.Parameters.AddWithValue("@software_id", null).SourceColumn = "?"; // I want to set this to be set to my 'software' table to the 'id' column.
// *****
sqlDA.InsertCommand = DAInsertCommand;
sqlDA.Update(updatedDs, "HS Join");

どこが間違っているのか、どうすればこれを克服できるのか教えてください。どうもありがとう!:)

4

1 に答える 1

1

あなたのコメントに関して言えば、これは、あなたと私が隣り合って座っていれば解決するような機会の 1 つに思えますが、少しトリッキーです。

これは、SqlConnection と SqlCommand を操作するときに使用したコードです。あなたのお役に立てる内容がここにあるかもしれません。

    public static void RunSqlCommandText(string connectionString, string commandText) {
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand comm = conn.CreateCommand();

        try {
            comm.CommandType = CommandType.Text;
            comm.CommandText = commandText;

            comm.Connection = conn;
            conn.Open();
            comm.ExecuteNonQuery();
        } catch (Exception ex) {
            System.Diagnostics.EventLog el = new System.Diagnostics.EventLog();
            el.Source = "data access class";
            el.WriteEntry(ex.Message + ex.StackTrace + " SQL '" + commandText + "'");
        } finally {
            conn.Close();
            comm.Dispose();
        }
    }


    public static int RunSqlAndReturnId(string connectionString, string commandText) {
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand comm = conn.CreateCommand();
        int id = -1;

        try {
            comm.CommandType = CommandType.Text;
            comm.CommandText = commandText;

            comm.Connection = conn;
            conn.Open();
            var returnvalue = comm.ExecuteScalar();
            if (returnvalue != null) {
                id = (int)returnvalue;                
            }
        } catch (Exception ex) {
            System.Diagnostics.EventLog el = new System.Diagnostics.EventLog();
            el.Source = "data access class";
            el.WriteEntry(ex.Message + ex.StackTrace + " SQL '" + commandText + "'");
        } finally {
            conn.Close();
            comm.Dispose();
        }

        return id;
    }
于 2013-01-17T14:01:46.873 に答える