简体   繁体   中英

ADO.Net INSERT not inserting data

I've got ac#.Net 4.5 console application and I'm trying to insert data from a DataTable to a SQL Server 2008R2 database table. I get no errors, but no data gets inserted. Here's my code:

Int32 newID = 0;
DataTable dtMaxUserID = GeneralClassLibrary.GeneralDataAccessLayer.ExecuteSelect("SELECT MAX(UserID)+1 AS NewID FROM TIUser", false, "TrackitCN");
newID = Convert.ToInt32(dtMaxUserID.Rows[0]["NewID"].ToString());

//Get new users from AllUserData
DataColumn dcRowID = new DataColumn("RowID", typeof(Int32));
//dcRowID.AllowDBNull = false;
dcRowID.AutoIncrement = true;
dcRowID.AutoIncrementSeed = 1;
dcRowID.AutoIncrementStep = 1;
//dcRowID.Unique = true;
//dcRowID.ColumnName = "RowID";
DataTable dtNewUsers = new DataTable();
dtNewUsers.Columns.Add(dcRowID);
dtNewUsers.Columns.Add("MaxID");
dtNewUsers.Columns.Add("UserID");
dtNewUsers.Columns.Add("FullName");
dtNewUsers.Columns.Add("Title");
dtNewUsers.Columns.Add("Phone");
dtNewUsers.Columns.Add("EMailAddr");
dtNewUsers.Columns.Add("Fax");
dtNewUsers.Columns.Add("Dept");
dtNewUsers.Columns.Add("Dept_Num");
dtNewUsers.Columns.Add("Location");
dtNewUsers.Columns.Add("UserDef_1");
dtNewUsers.Columns.Add("UserDef_2");
dtNewUsers.Columns.Add("Login");
dtNewUsers.Columns.Add("Password");
dtNewUsers.Columns.Add("PasswordFlags");
dtNewUsers.Columns.Add("LanguageID");
dtNewUsers.Columns.Add("NTAuthentication");
dtNewUsers.Columns.Add("NTAccount");
dtNewUsers.Columns.Add("SID");
dtNewUsers.Columns.Add("SelfServiceAccess");
dtNewUsers.Columns.Add("ImagePath");
dtNewUsers.Columns.Add("CompFlag");
dtNewUsers.Columns.Add("Employee_ID");
dtNewUsers.Columns.Add("SessionID");
string strQuery = "SELECT " + newID + " AS MaxID, LName" + ", " + "FName AS FullName, Title, Phone, EMail AS EmailAddr, Fax, Department AS Dept, Office AS Location, [Login] AS Employee_ID FROM [User] WHERE StartDate >= CAST(DATEPART(year,getdate()) AS varchar) + '-' + CAST(DATEPART(month,getdate()) AS varchar) + '-' + CAST(DATEPART(day,getdate())-1 AS varchar)";
DataTable dtTemp = GeneralClassLibrary.GeneralDataAccessLayer.ExecuteSelect(strQuery, false, "AllUserDataCN");
foreach (DataRow row in dtTemp.Rows)
    dtNewUsers.ImportRow(row);

SqlTransaction tran = null;
    try
    {
        connection.Open();
        tran = connection.BeginTransaction();

        //Insert new users into TIUser
        SqlDataAdapter TIUser_adapter = new SqlDataAdapter();
        string queryString = "SELECT * FROM TIUser WHERE 1 = 0";
        TIUser_adapter.SelectCommand = new SqlCommand(queryString, connection, tran);
        TIUser_adapter.Fill(dtNewUsers);
        dtNewUsers.AcceptChanges();
        TIUser_adapter.Update(dtNewUsers);

        tran.Commit();
    }
    catch (System.Exception ex)
    {
        tran.Rollback();
        throw ex;
    }
    finally
    {
        connection.Close();
    }

GeneralClassLibrary is a class library we use here for a number of things; here it just executes a SELECT statement on a database. TIUser is the database table. The DataTable, dtNewUsers contains one row. I've verified that by debugging the code and inspecting the DataTable after the ImportRow is done.


After following the reply from user3787557 (THANK YOU!), I'm closer, but I'm getting a concurrency violation. I'm working on a development database, and all I'm doing is inserting a record, so I have no idea why there'd be a concurrency violation. One possibility: I alter the structure of the DataTable dtNewUsers by adding two columns. However, before I do the update, I remove those columns. The InsertCommand is fine; I've checked it in SSMS and it parses. Here's my new code:

using (SqlConnection connection = new SqlConnection(GeneralClassLibrary.GeneralConfigurationManager.ConnectionStrings["TrackitCN"].ConnectionString))
        {
            SqlTransaction tran = null;
            connection.Open();
            tran = connection.BeginTransaction();

            try
            {
                //Create empty TIUser Data Adapter
                SqlDataAdapter TIUser_adapter = new SqlDataAdapter();
                SqlCommandBuilder TIUser_builder = new SqlCommandBuilder(TIUser_adapter);
                string queryString = "SELECT * FROM TIUser WHERE 1 = 0";
                TIUser_adapter.SelectCommand = new SqlCommand(queryString, connection, tran);
                TIUser_adapter.InsertCommand = TIUser_builder.GetInsertCommand();
                TIUser_adapter.UpdateCommand = TIUser_builder.GetUpdateCommand();

                //Get new users from AllUserData
                DataTable dtNewUsers = new DataTable();
                TIUser_adapter.Fill(dtNewUsers);
                DataColumn dcRowID = new DataColumn("RowID", typeof(Int32));
                dcRowID.AutoIncrement = true;
                dcRowID.AutoIncrementSeed = 1;
                dcRowID.AutoIncrementStep = 1;
                dtNewUsers.Columns.Add(dcRowID);
                dtNewUsers.Columns.Add("MaxID");                                        string strQuery = "SELECT " + newID + " AS MaxID, LName + ', ' + FName AS FullName, Title, Phone, EMail AS EmailAddr, Fax, Department AS Dept, Office AS Location, [Login] AS Employee_ID FROM [User] WHERE StartDate >= CAST(DATEPART(year,getdate()) AS varchar) + '-' + CAST(DATEPART(month,getdate()) AS varchar) + '-' + CAST(DATEPART(day,getdate())-1 AS varchar)";
                DataTable dtTemp = GeneralClassLibrary.GeneralDataAccessLayer.ExecuteSelect(strQuery, false, "AllUserDataCN");
                foreach (DataRow row in dtTemp.Rows)
                    dtNewUsers.ImportRow(row);

                //Make sure new users aren't already in Trackit
                foreach (DataRow row in dtNewUsers.Rows)
                {
                    row["UserID"] = (Convert.ToInt32(row["RowID"]) + Convert.ToInt32(row["MaxID"])).ToString();
                    DataTable dtOverlap = GeneralClassLibrary.GeneralDataAccessLayer.ExecuteSelect("SELECT * FROM TIUser WHERE Employee_ID = '" + row["Employee_ID"].ToString() + "'", false, "TrackitCN");
                    if (dtOverlap.Rows.Count > 0)
                        dtNewUsers.Rows.Remove(row);
                }    

                //Remove MaxID and RowID Columns
                dtNewUsers.Columns.Remove("MaxID");
                dtNewUsers.Columns.Remove("RowID")

                TIUser_adapter.Update(dtNewUsers);
                tran.Commit();
            }
            catch (System.Exception ex)
            {
                GeneralClassLibrary.GeneralEmail.ExceptionNotification(System.Reflection.Assembly.GetExecutingAssembly().ToString(), ex.Message, ex.StackTrace);
                tran.Rollback();
                throw ex;
            }
            finally
            {
                connection.Close();
            }
        }

The Stack Trace I get is:

   at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)


at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)



at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)



at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)



at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)



at UpdateTrackitUsers.Program.Main(String[] args) in c:\Users\06717\Documents\Visual Studio 2012\Projects\UpdateTrackitUsersConsole\UpdateTrackitUsersConsole\Program.cs:line 91

Line 87 is the line:

TIUser_adapter.Update(dtNewUsers);

This is the code that worked for me. Basically what you're missing on your code is to use SqlCommandBuilder to create InsertCommand and UpdateCommand. Also GET RID of AcceptChanges(). That is going to cause the new row to NOT be sent to the database. One last thing: make sure you mark the MDF database file (if you're using one) properties with "Do Not Copy" otherwise the database gets overridden every time you compile and you can't see your changes.

using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlDataAdapter adapter = new SqlDataAdapter();
    SqlCommandBuilder builder = new SqlCommandBuilder(adapter);

    adapter.SelectCommand = new SqlCommand(queryString, connection);
    adapter.InsertCommand = builder.GetInsertCommand();
    adapter.UpdateCommand = builder.GetUpdateCommand();
    adapter.DeleteCommand = builder.GetDeleteCommand();

    DataTable dt = new DataTable();
    adapter.Fill(dt);

    DataRow row = dt.NewRow();
    row["RegionID"] = 5;
    row["RegionDescription"] = "Some region";
    dt.Rows.Add(row);

    //dt.AcceptChanges();

    int counter = adapter.Update(dt); 
}                    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM