在WPF中插入多个值的单个语句 [英] Single Statement to insert multiple values in WPF

查看:69
本文介绍了在WPF中插入多个值的单个语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




是否可以通过单个语句插入多个值
即通过单个"add new Sqlparametr"语句将值分配给参数


我在单独的语句中编写了代码以添加参数值

我写了如下代码

字符串str =插入State(Code,Name)值(@ Statecode1,@ Statename1)";
cmd.Parameters.Add(新SqlParameter("Statecode1",this.txtstatecode.Text));
cmd.Parameters.Add(新的SqlParameter("Statename1",this.txtstatename.Text));
cmd.CommandText = str;
cmd.Connection = conn;
cmd.ExecuteNonQuery();

Hi,


Is it possible to insert multiple values through single statement
That is to assign the values to the parameter through single "add new Sqlparametr" statement


I wrote code in separate statements to add the parameter values

I wrote code as follows

String str = "insert into State(Code,Name)values (@Statecode1,@Statename1)";
cmd.Parameters.Add(new SqlParameter("Statecode1", this.txtstatecode.Text));
cmd.Parameters.Add (new SqlParameter ("Statename1", this.txtstatename.Text));
cmd.CommandText = str;
cmd.Connection = conn;
cmd.ExecuteNonQuery();

推荐答案

首先,这与WPF无关.没事接下来,如果您创建一个函数来为您执行此操作,则可以使用单个语句添加多个参数:
First of all, this has nothing to do with WPF. Nothing at all. Moving on, you can add multiple parameters with a single statement, if you create a function to do that for you:
public void AddParameters(SqlCommand cmd, params SqlParameter[] parameters)
{
	foreach (SqlParameter p in parameters)
	{
		cmd.Parameters.Add(p);
	}
}


您可以这样称呼:


You can call that like this:

SqlCommand cmd = new SqlCommand("SELECT * FROM MyTable WHERE C1=@P1 AND C2=@P2");
AddParameters(cmd, new SqlParameter("P1", "..."), new SqlParameter("P2", "..."));


通过使用 params 关键字,您可以将任意数量的SQL参数传递给该函数.


By using the params keyword, you can pass as many SQL parameters as you want to the function.


这篇关于在WPF中插入多个值的单个语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆