Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

I do the following below for inserting in a table, but now with Core2, SqlCommandBuilder doesn't work? Is there a better apporach?

$
0
0
public static void InsertANewItemInAForeignTableWithAssetID(string strConnection, string strSQL, int intID, string strColumnSaveTo, object objValueToSave)
        {
            try
            {
                using (SqlConnection objConnection = new SqlConnection(strConnection))
                {
                    using (SqlCommand objCommand = new SqlCommand(strSQL, objConnection))
                    {
                        SqlDataAdapter objAdapter = new SqlDataAdapter(objCommand);

                        objCommand.CommandType = CommandType.StoredProcedure;

                        DataSet objDataset = new DataSet();
                        DataTable objTable = new DataTable();

                        objConnection.Open();

                        objAdapter.FillSchema(objDataset, SchemaType.Source, "Table");
                        objAdapter.Fill(objDataset, "Table");
                        objTable = objDataset.Tables["Table"];

                        DataColumn[] objPrimaryKey = new DataColumn[1];

                        objTable.PrimaryKey = objPrimaryKey;

                        DataRow objRow = objTable.NewRow();
                        objRow[strColumnSaveTo] = objValueToSave;
                        objRow["ID"] = intID;
                        objTable.Rows.Add(objRow);

                        SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(objAdapter);
                        objAdapter.InsertCommand = objCommandBuilder.GetInsertCommand();
                        objAdapter.Update(objDataset, "Table");
                    }
                }
           }

Viewing all articles
Browse latest Browse all 9386

Trending Articles