tags:

views:

41

answers:

3

Hi All,

if have the following SQLCommand which should do an Update. The issue is, that I get no errors but it still do not update the database ?

        SqlConnection sqlconn2 = new SqlConnection(this.connectionString);
        sqlconn2.Open();

        string strCmd = "UPDATE dbo.mydata SET WEB_OBEZ1 = @OBEZ1, WEB_OBEZ2 = @OBEZ2, WEB_OBEZ3 = @OBEZ3 WHERE O_KURZ = @OKURZ";

        using (SqlCommand cmd2 = new SqlCommand(strCmd, sqlconn2))
        {
            cmd2.Parameters.Add("@OBEZ1", SqlDbType.NVarChar);
            cmd2.Parameters.Add("@OBEZ2", SqlDbType.NVarChar);
            cmd2.Parameters.Add("@OBEZ3", SqlDbType.NVarChar);
            cmd2.Parameters.Add("@OKURZ", SqlDbType.NVarChar);


            foreach (DataRow dr in dt.Rows)
            {
                // Felder holen
                string okuerzel = dr["O_KURZ"].ToString();
                string bezeichnung = dr["O_BEZ"].ToString();


                    string[] lines = CreateNewOrgBez(bezeichnung);

                    cmd2.Parameters["@OBEZ1"].Value = lines[0];
                    cmd2.Parameters["@OBEZ2"].Value = lines[1];
                    cmd2.Parameters["@OBEZ3"].Value = lines[2];
                    cmd2.Parameters["@OKURZ"].Value = okuerzel;
                    cmd2.ExecuteNonQuery();

            }
        }
        sqlconn2.Close();
+2  A: 

Perhaps the length of dt.Rows is zero. have you checked for this?

RedFilter
No tested it with a counter. dt.Rows contains 19000 objects and the foreach also runs 19000 times. So I have enaugh objects and the foreach works fine.
WorldSignia
A: 

using transactions? then perhapse a commit is missing?

hth

Mario

Mario The Spoon
You should merge your two answers into one Mario.
Abe Miessler
A: 

Perhapse also the where clause did not match any row in the db?

ExecuteNonQuery() returns the number of rows affected, check it!

hth

Mario

Mario The Spoon