0

几天来,我一直在浏览互联网上的页面和页面,尝试不同的方法,但我仍然不确定我应该如何做到这一点。

在我的第三个 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

关于您的评论,这似乎是其中一种情况,如果您和我坐在一起,我们会对此进行排序,但这有点棘手。

这是我在使用 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 回答