Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
C# ADO Executing Stored Procedure Problem
Message
 
À
19/01/2009 00:04:00
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01374697
Message ID:
01375230
Vues:
6
Ok, I think I see it now...

You wrote:
	SqlCommand sc = new SqlCommand();
	sc.Connection = this.oConnection;
	sc.Connection.Open();
	foreach (DataRow Row in ds.Tables[0].Rows)
	{
		switch (Row.RowState)
		{
			case DataRowState.Added :
				// or use a StoredProc, which I prefer
				sc.CommandText = "Insert into bob (xyz, abc) VALUES ( @xyz, @abc )";
				sc.Parameters.Clear();
				sc.Parameters.Add("@xyz", Row["xyz"]);
				sc.Parameters.Add("@abc", Row["abc"]);
				sc.ExecuteNonQuery();
				break;
				
			// Do the same for DataRowState Deleted and Modified
			case DataRowState.Deleted :
				break;
			case DataRowState.Modified :
				break;
		}
	}
	sc.Connection.Close();
But this line...
sc.CommandText = "Insert into bob (xyz, abc) VALUES ( @xyz, @abc )";
could be up here instead....
SqlCommand sc = new SqlCommand();
sc.CommandText = "Insert into bob (xyz, abc) VALUES ( @xyz, @abc )";  <<<---------------
sc.Connection = this.oConnection;
sc.Connection.Open();
foreach (DataRow Row in ds.Tables[0].Rows)
{
.
.
.
Then the code in the foreach only needs the param info, right?



>>Now I'm confused.
>>
>>You have:
>
>
>>sc.CommandText = "Insert into bob (xyz, abc) VALUES ( @xyz, @abc )";
>>sc.Parameters.Clear();
>>sc.Parameters.Add("@xyz", Row["xyz"]);
>>sc.Parameters.Add("@abc", Row["abc"]);
>>sc.ExecuteNonQuery();
>
>
>>You could just as easily do:
>
>>sc.CommandText = "Insert into bob (xyz, abc) VALUES ( @xyz, @abc )";
>>sc.ExecuteNonQuery();
>
>
>>So what do the parameters do?

>
>
>
>Think about what you just wrote Kevin. What good is
>
>"Insert into bob (xyz, abc) VALUES ( @xyz, @abc )";
>
>
>when @xyz and @abc aren't defined as anything?!?!?
>
>That's what this does, defines and adds the parameters to the Command object:
>
>
>sc.Parameters.Clear();
>sc.Parameters.Add("@xyz", Row["xyz"]);
>sc.Parameters.Add("@abc", Row["abc"]);
>
>
>~~Bonnie
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform