在接下來的程序員開始了,這是我的完整的解決方案編輯DataGridView的必然數據表時自動進行更新SQL數據庫/表:
綁定:(聯繫在了一起dgView, dTable,& PositionChange事件)
private void tvVX130_AfterSelect(object sender, TreeViewEventArgs e)
{
string t = tvVX130.SelectedNode.Text;
BindingSource bs1 = new BindingSource();
bs1.PositionChanged += bindingSource1_PositionChanged;
bs1.DataSource = tblvAttributes;
dgvVX130.DataSource = bs1;
string dwTN = tvVX130.SelectedNode.Text.Substring(0, tvVX130.SelectedNode.Text.IndexOf(" - "));
bs1.Filter = "DWPhysicalTableName = '" + dwTN + "' AND DWPhysicalSchemaName = '" + t.Substring(t.IndexOf(" - ") + 5) + "'";
dgvVX130.DataSource = bs1;
}
創建從其中執行適配器更新事件: 私人無效bindingSource1_PositionChanged(對象發件人,EventArgs的) { var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); AppSettingsSection appSettingSection =(AppSettingsSection)config.GetSection(「cbSettings」); SqlConnection vx130 = new SqlConnection(appSettingSection.Settings [cbRegion.SelectedItem.ToString()] Value); SqlDataAdapter da = CreateSQLAdapter(vx130); da.Update(tblvAttributes); }
設置SQL適配器:(所有的好處都是,也是冗長的,另一種方法是重新調整語句來調用SQL存儲過程,但我沒有。)
public static SqlDataAdapter CreateSQLAdapter(SqlConnection vx130)
{
SqlDataAdapter da = new SqlDataAdapter();
// Create the SelectCommand.
SqlCommand command = new SqlCommand("Select DatabaseName, DWPhysicalSchemaName, DWPhysicalTableName, " +
"DWFieldName ,DataDomain, DWFieldDataType, DWFieldLength, DWFieldScale, SourceAttributeSID, "+
"ResolvedValue, PointedToField, MapComments, PrimaryKeyEntitySID, SpecialHandlingFlag, "+
"DWFieldTechnicalDescription, BuildStatus from meta.attributemap", vx130);
da.SelectCommand = command;
// Create the InsertCommand.
command = new SqlCommand(
"Insert Into [Meta].[AttributeMap] " +
"(DatabaseName, DWPhysicalSchemaName, DWPhysicalTableName, " +
"DWFieldName ,DataDomain, DWFieldDataType, DWFieldLength, DWFieldScale, SourceAttributeSID, " +
"ResolvedValue, PointedToField, MapComments, PrimaryKeyEntitySID, SpecialHandlingFlag, " +
"DWFieldTechnicalDescription, BuildStatus) " +
"Values (@DatabaseName, @DWPhysicalSchemaName, @DWPhysicalTableName, " +
"@DWFieldName ,@DataDomain, @DWFieldDataType, @DWFieldLength, @DWFieldScale, @SourceAttributeSID, " +
"@ResolvedValue, @PointedToField, @MapComments, @PrimaryKeyEntitySID, @SpecialHandlingFlag, " +
"@DWFieldTechnicalDescription, @BuildStatus)" , vx130);
// Add the parameters for the InsertCommand.
command.Parameters.Add(new SqlParameter("@DatabaseName", SqlDbType.VarChar));
command.Parameters["@DatabaseName"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DatabaseName"].SourceColumn = "DatabaseName";
command.Parameters.Add(new SqlParameter("@DWPhysicalSchemaName", SqlDbType.VarChar));
command.Parameters["@DWPhysicalSchemaName"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DWPhysicalSchemaName"].SourceColumn = "DWPhysicalSchemaName";
command.Parameters.Add(new SqlParameter("@DWPhysicalTableName", SqlDbType.VarChar));
command.Parameters["@DWPhysicalTableName"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DWPhysicalTableName"].SourceColumn = "DWPhysicalTableName";
command.Parameters.Add(new SqlParameter("@DWFieldName", SqlDbType.VarChar));
command.Parameters["@DWFieldName"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DWFieldName"].SourceColumn = "DWFieldName";
command.Parameters.Add(new SqlParameter("@DataDomain", SqlDbType.VarChar));
command.Parameters["@DataDomain"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DataDomain"].SourceColumn = "DataDomain";
command.Parameters.Add(new SqlParameter("@DWFieldDataType", SqlDbType.VarChar));
command.Parameters["@DWFieldDataType"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DWFieldDataType"].SourceColumn = "DWFieldDataType";
command.Parameters.Add(new SqlParameter("@DWFieldLength", SqlDbType.VarChar));
command.Parameters["@DWFieldLength"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DWFieldLength"].SourceColumn = "DWFieldLength";
command.Parameters.Add(new SqlParameter("@DWFieldScale", SqlDbType.Int));
command.Parameters["@DWFieldScale"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DWFieldScale"].SourceColumn = "DWFieldScale";
command.Parameters.Add(new SqlParameter("@SourceAttributeSID", SqlDbType.Int));
command.Parameters["@SourceAttributeSID"].SourceVersion = DataRowVersion.Current;
command.Parameters["@SourceAttributeSID"].SourceColumn = "SourceAttributeSID";
command.Parameters.Add(new SqlParameter("@ResolvedValue", SqlDbType.VarChar));
command.Parameters["@ResolvedValue"].SourceVersion = DataRowVersion.Current;
command.Parameters["@ResolvedValue"].SourceColumn = "ResolvedValue";
command.Parameters.Add(new SqlParameter("@PointedToField", SqlDbType.VarChar));
command.Parameters["@PointedToField"].SourceVersion = DataRowVersion.Current;
command.Parameters["@PointedToField"].SourceColumn = "PointedToField";
command.Parameters.Add(new SqlParameter("@MapComments", SqlDbType.VarChar));
command.Parameters["@MapComments"].SourceVersion = DataRowVersion.Current;
command.Parameters["@MapComments"].SourceColumn = "MapComments";
command.Parameters.Add(new SqlParameter("@PrimaryKeyEntitySID", SqlDbType.Int));
command.Parameters["@PrimaryKeyEntitySID"].SourceVersion = DataRowVersion.Current;
command.Parameters["@PrimaryKeyEntitySID"].SourceColumn = "PrimaryKeyEntitySID";
command.Parameters.Add(new SqlParameter("@SpecialHandlingFlag", SqlDbType.VarChar));
command.Parameters["@SpecialHandlingFlag"].SourceVersion = DataRowVersion.Current;
command.Parameters["@SpecialHandlingFlag"].SourceColumn = "SpecialHandlingFlag";
command.Parameters.Add(new SqlParameter("@DWFieldTechnicalDescription", SqlDbType.VarChar));
command.Parameters["@DWFieldTechnicalDescription"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DWFieldTechnicalDescription"].SourceColumn = "DWFieldTechnicalDescription";
command.Parameters.Add(new SqlParameter("@BuildStatus", SqlDbType.VarChar));
command.Parameters["@BuildStatus"].SourceVersion = DataRowVersion.Current;
command.Parameters["@BuildStatus"].SourceColumn = "BuildStatus";
da.InsertCommand = command;
// Create the UpdateCommand.
command = new SqlCommand(
"UPDATE [Meta].[AttributeMap] "+
"SET DatabaseName = @DatabaseName, DWPhysicalSchemaName = @DWPhysicalSchemaName, " +
"[email protected], [email protected], [email protected]," +
"[email protected], [email protected], [email protected]," +
"[email protected], [email protected], @[email protected]," +
"[email protected], [email protected], [email protected]," +
"[email protected], [email protected] " +
"WHERE DWPhysicalSchemaName = @DWPhysicalSchemaName and DWPhysicalTableName= @DWPhysicalTableName and [email protected]", vx130);
command.Parameters.Add(new SqlParameter("@DatabaseName", SqlDbType.VarChar));
command.Parameters["@DatabaseName"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DatabaseName"].SourceColumn = "DatabaseName";
command.Parameters.Add(new SqlParameter("@DWPhysicalSchemaName", SqlDbType.VarChar));
command.Parameters["@DWPhysicalSchemaName"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DWPhysicalSchemaName"].SourceColumn = "DWPhysicalSchemaName";
command.Parameters.Add(new SqlParameter("@DWPhysicalTableName", SqlDbType.VarChar));
command.Parameters["@DWPhysicalTableName"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DWPhysicalTableName"].SourceColumn = "DWPhysicalTableName";
command.Parameters.Add(new SqlParameter("@DWFieldName", SqlDbType.VarChar));
command.Parameters["@DWFieldName"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DWFieldName"].SourceColumn = "DWFieldName";
command.Parameters.Add(new SqlParameter("@DataDomain", SqlDbType.VarChar));
command.Parameters["@DataDomain"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DataDomain"].SourceColumn = "DataDomain";
command.Parameters.Add(new SqlParameter("@DWFieldDataType", SqlDbType.VarChar));
command.Parameters["@DWFieldDataType"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DWFieldDataType"].SourceColumn = "DWFieldDataType";
command.Parameters.Add(new SqlParameter("@DWFieldLength", SqlDbType.VarChar));
command.Parameters["@DWFieldLength"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DWFieldLength"].SourceColumn = "DWFieldLength";
command.Parameters.Add(new SqlParameter("@DWFieldScale", SqlDbType.Int));
command.Parameters["@DWFieldScale"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DWFieldScale"].SourceColumn = "DWFieldScale";
command.Parameters.Add(new SqlParameter("@SourceAttributeSID", SqlDbType.Int));
command.Parameters["@SourceAttributeSID"].SourceVersion = DataRowVersion.Current;
command.Parameters["@SourceAttributeSID"].SourceColumn = "SourceAttributeSID";
command.Parameters.Add(new SqlParameter("@ResolvedValue", SqlDbType.VarChar));
command.Parameters["@ResolvedValue"].SourceVersion = DataRowVersion.Current;
command.Parameters["@ResolvedValue"].SourceColumn = "ResolvedValue";
command.Parameters.Add(new SqlParameter("@PointedToField", SqlDbType.VarChar));
command.Parameters["@PointedToField"].SourceVersion = DataRowVersion.Current;
command.Parameters["@PointedToField"].SourceColumn = "PointedToField";
command.Parameters.Add(new SqlParameter("@MapComments", SqlDbType.VarChar));
command.Parameters["@MapComments"].SourceVersion = DataRowVersion.Current;
command.Parameters["@MapComments"].SourceColumn = "MapComments";
command.Parameters.Add(new SqlParameter("@PrimaryKeyEntitySID", SqlDbType.Int));
command.Parameters["@PrimaryKeyEntitySID"].SourceVersion = DataRowVersion.Current;
command.Parameters["@PrimaryKeyEntitySID"].SourceColumn = "PrimaryKeyEntitySID";
command.Parameters.Add(new SqlParameter("@SpecialHandlingFlag", SqlDbType.VarChar));
command.Parameters["@SpecialHandlingFlag"].SourceVersion = DataRowVersion.Current;
command.Parameters["@SpecialHandlingFlag"].SourceColumn = "SpecialHandlingFlag";
command.Parameters.Add(new SqlParameter("@DWFieldTechnicalDescription", SqlDbType.VarChar));
command.Parameters["@DWFieldTechnicalDescription"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DWFieldTechnicalDescription"].SourceColumn = "DWFieldTechnicalDescription";
command.Parameters.Add(new SqlParameter("@BuildStatus", SqlDbType.VarChar));
command.Parameters["@BuildStatus"].SourceVersion = DataRowVersion.Current;
command.Parameters["@BuildStatus"].SourceColumn = "BuildStatus";
da.UpdateCommand = command;
// Create the DeleteCommand.
command = new SqlCommand(
"delete from vx130.Meta.AttributeMap " +
" where DWPhysicalSchemaName = @DWPhysicalSchemaName AND " +
" DWPhysicalTableName = @DWPhysicalTableName AND DWFieldName = @DWFieldName", vx130);
// Add the parameters for the DeleteCommand.
command.Parameters.Add(new SqlParameter("@DWPhysicalSchemaName", SqlDbType.VarChar));
command.Parameters["@DWPhysicalSchemaName"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DWPhysicalSchemaName"].SourceColumn = "DWPhysicalSchemaName";
command.Parameters.Add(new SqlParameter("@DWPhysicalTableName", SqlDbType.VarChar));
command.Parameters["@DWPhysicalTableName"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DWPhysicalTableName"].SourceColumn = "DWPhysicalTableName";
command.Parameters.Add(new SqlParameter("@DWFieldName", SqlDbType.VarChar));
command.Parameters["@DWFieldName"].SourceVersion = DataRowVersion.Current;
command.Parameters["@DWFieldName"].SourceColumn = "DWFieldName";
da.DeleteCommand = command;
return da;
}
}
}
此行:command.Parameters.AddWithValue(「@ DWFieldScale」,「DWFieldScale」);如果DWFieldScale是一個int,則會發生錯誤,因爲您將值作爲值傳遞給字段「DWFieldScale」。命令背後的想法。參數控制是爲了進行任何需要的轉換。 – Jauch 2014-12-08 00:36:58
當你找到有用的答案,upvote他們。不要忘記選擇「解決」你的問題或懷疑的答案。這有助於其他用戶和願意幫助的人:) – Jauch 2014-12-08 00:53:37