Using SqlCommand , how to add multiple parameters to its object , insertion via winform in sql table?

You can use an extension method, like this.

You can use an extension method, like this: public static class DbCommandExtensions { public static void AddInputParameters(this IDbCommand cmd, T parameters) where T : class { foreach (var prop in parameters.GetType().GetProperties()) { object val = prop. GetValue(parameters, null); var p = cmd.CreateParameter(); p. ParameterName = prop.Name; p.

Value = val? DBNull. Value; cmd.Parameters.

Add(p); } } } Then call it like this: cmd. AddInputParameters(new { a = textBox1. Text, be = TextBox2.

Text, /* etc */ }); I've used that in a few projects with no problems.

Sir , am using . Net 2.0 , so in place of 'var' and what shall I use? – sqlchild Apr 4 at 7:14 IDbCommand.

CreateParameter returns an IDbDataParameter, so declare "p" as that. Otherwise this should work fine in . NET 2.0 as far as I'm aware.

– Matt Hamilton Apr 4 at 7:36.

I think you can use Parameters.AddWithValue() method. Cmd.Parameters. AddWithValue("@j",textbox10.

Text); cmd.Parameters. AddWithValue("@k",textbox11. Text); cmd.Parameters.

AddWithValue("@l",textbox12. Text).

The 2 'solutions' that you suggest in your question, are semantically different. Which one you should use, depends on your table-layout. The first solution inserts one record in the table, the second insert statement inserts one record (row) for every value (textbox).

Difficult to give a good answer here, since we do not know what you're going to save in that table, and thus , we cannot say how you should save it (how you save it, is inherintly dependent on the way you should call the SQL insert statement).

Sir, I have edited my post – sqlchild Apr 4 at 6:52.

You could use a function like this: void AddParams(DBCommand cmd,params object parameters) { if (parameters! = null) { int index = 0; while (index AddWithValue("@"+(string)parametersindex, parametersindex + 1); index += 2; } } } Not the best one probably, but functional. Call link this: AddParams(a,"test1",b,3,c,DateTime.

Now); Or you can use an extension like suggested from @Matt Hamilton to add this function in DBCommand class.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions


Thank You!
send