I have ten textboxes in my winform, and i need to save the text typed in these textboxes into 10 columns of a sql database table. so, for this shall i write :
我的winform中有10个文本框,我需要将这些文本框中的文本保存到sql数据库表的10列中。为此,我要写:
INSERT INTO item (c1,c2,c3...,c10) values (@a,@b....@j)
cmd.Parameters.Add("@a",SqlDbType.Varchar)
cmd.Parameteres["@a"].Value=textbox1.Text;
cmd.Parameters.Add("@b",SqlDbType.Varchar)
cmd.Parameteres["@b"].Value=textbox2.Text;.
.
.
.
.
cmd.Parameters.Add("@j",SqlDbType.Varchar)
cmd.Parameteres["@j"].Value=textbox10.Text;
OR ten separate queries for each textbox:
或对每个文本框分别进行10个查询:
INSERT INTO item (c1) values (@a)
cmd.Parameters.Add("@a",SqlDbType.Varchar)
cmd.Parameteres["@a"].Value=textbox1.Text;
INSERT INTO item (c2) values (@b)
cmd.Parameters.Add("@b",SqlDbType.Varchar)
cmd.Parameteres["@b"].Value=textbox2.Text;.
.
.
INSERT INTO item (c10) values (@j)
cmd.Parameters.Add("@j",SqlDbType.Varchar)
cmd.Parameteres["@j"].Value=textbox10.Text;
or, please suggest an efficient code.
或者,请建议一个高效的代码。
How to add multiple parameters to cmd in a single statement? is it possible?
如何在一个语句中向cmd添加多个参数?是可能的吗?
4 个解决方案
#1
9
You can use an extension method, like this:
您可以使用扩展方法,如下所示:
public static class DbCommandExtensions
{
public static void AddInputParameters<T>(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, b = TextBox2.Text, /* etc */ });
I've used that in a few projects with no problems.
我在一些没有问题的项目中使用过它。
#2
9
I think you can use Parameters.AddWithValue() method.
我认为您可以使用Parameters.AddWithValue()方法。
cmd.Parameters.AddWithValue("@j",textbox10.Text);
cmd.Parameters.AddWithValue("@k",textbox11.Text);
cmd.Parameters.AddWithValue("@l",textbox12.Text);
#3
2
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).
在这里很难给出一个好的答案,因为我们不知道您将在该表中保存什么,因此,我们不能说您应该如何保存它(您如何保存它,它是基于您应该调用SQL insert语句的方式来继承的)。
#4
0
You could use a function like this:
你可以使用如下函数:
void AddParams(DBCommand cmd,params object[] parameters)
{
if (parameters != null)
{
int index = 0;
while (index < parameters.Length)
{
cmd.Parameters.AddWithValue("@"+(string)parameters[index], parameters[index + 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.
或者可以使用@Matt Hamilton建议的扩展名在DBCommand类中添加这个函数。
#1
9
You can use an extension method, like this:
您可以使用扩展方法,如下所示:
public static class DbCommandExtensions
{
public static void AddInputParameters<T>(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, b = TextBox2.Text, /* etc */ });
I've used that in a few projects with no problems.
我在一些没有问题的项目中使用过它。
#2
9
I think you can use Parameters.AddWithValue() method.
我认为您可以使用Parameters.AddWithValue()方法。
cmd.Parameters.AddWithValue("@j",textbox10.Text);
cmd.Parameters.AddWithValue("@k",textbox11.Text);
cmd.Parameters.AddWithValue("@l",textbox12.Text);
#3
2
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).
在这里很难给出一个好的答案,因为我们不知道您将在该表中保存什么,因此,我们不能说您应该如何保存它(您如何保存它,它是基于您应该调用SQL insert语句的方式来继承的)。
#4
0
You could use a function like this:
你可以使用如下函数:
void AddParams(DBCommand cmd,params object[] parameters)
{
if (parameters != null)
{
int index = 0;
while (index < parameters.Length)
{
cmd.Parameters.AddWithValue("@"+(string)parameters[index], parameters[index + 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.
或者可以使用@Matt Hamilton建议的扩展名在DBCommand类中添加这个函数。