报错:
The variable name '@Name' has already been declared. Variable names must be unique within a query batch or stored procedure.document All:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;namespace ImportExportFiles
{
public partial class Form1 : Form
{
public Form1 ()
{
InitializeComponent();
} private void btnImport_Click (object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
if (ofImport.ShowDialog() != DialogResult.OK) //如果用户选择取消,则退出.
{
return;
}
using (SqlCommand cmd = conn.CreateCommand())
{
//从指定路径读取文件.
using (FileStream fileStream = File.OpenRead(ofImport.FileName))
{
using (StreamReader streamReader = new StreamReader(fileStream))
{
string line = null; //存储从文件中读取的每行的信息.
while (null != (line = streamReader.ReadLine())) //不断读取,知道行中没有数据.
{
string[] people = line.Split('|'); //获取每个成员信息.
string name = people[0].Trim(); //提取每个成员的姓名部分,如果有空格,去除.
int age = Convert.ToInt32(people[1].Trim()); //提取每个成员的年龄部分,如果有空格,去除.
//用提取参数的方法将每个成员的信息写入表中.
cmd.CommandText = "insert into Peoples(Name,Age) values(@Name,@Age)";
cmd.Parameters.Add(new SqlParameter("Name", name));
cmd.Parameters.Add(new SqlParameter("Age", age));
cmd.ExecuteNonQuery(); //这里报错!
}
}
}
}
MessageBox.Show("Import Succeed!");
}
}
}
}我的需求是:
在 winfrom 中,单击 “导入”按钮,就弹出一个对话框(让用户选择要读取的文件),提取文件每行的“ Name” 和“Age”部分,然后通过连接 SQLServer,添加到我的表“People"表!
这个问题我碰到两次了。
明明的的程序中就只有一个 Name 属性(那个 @Name占位符又不算),总是说的已经声明多 Name)。
到底是什么歌原因哈!谢谢大家的时间!
The variable name '@Name' has already been declared. Variable names must be unique within a query batch or stored procedure.document All:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;namespace ImportExportFiles
{
public partial class Form1 : Form
{
public Form1 ()
{
InitializeComponent();
} private void btnImport_Click (object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
if (ofImport.ShowDialog() != DialogResult.OK) //如果用户选择取消,则退出.
{
return;
}
using (SqlCommand cmd = conn.CreateCommand())
{
//从指定路径读取文件.
using (FileStream fileStream = File.OpenRead(ofImport.FileName))
{
using (StreamReader streamReader = new StreamReader(fileStream))
{
string line = null; //存储从文件中读取的每行的信息.
while (null != (line = streamReader.ReadLine())) //不断读取,知道行中没有数据.
{
string[] people = line.Split('|'); //获取每个成员信息.
string name = people[0].Trim(); //提取每个成员的姓名部分,如果有空格,去除.
int age = Convert.ToInt32(people[1].Trim()); //提取每个成员的年龄部分,如果有空格,去除.
//用提取参数的方法将每个成员的信息写入表中.
cmd.CommandText = "insert into Peoples(Name,Age) values(@Name,@Age)";
cmd.Parameters.Add(new SqlParameter("Name", name));
cmd.Parameters.Add(new SqlParameter("Age", age));
cmd.ExecuteNonQuery(); //这里报错!
}
}
}
}
MessageBox.Show("Import Succeed!");
}
}
}
}我的需求是:
在 winfrom 中,单击 “导入”按钮,就弹出一个对话框(让用户选择要读取的文件),提取文件每行的“ Name” 和“Age”部分,然后通过连接 SQLServer,添加到我的表“People"表!
这个问题我碰到两次了。
明明的的程序中就只有一个 Name 属性(那个 @Name占位符又不算),总是说的已经声明多 Name)。
到底是什么歌原因哈!谢谢大家的时间!
解决方案 »
- UdpClient 作为客户端连接服务器
- c#中的两个符号
- C#操作文本文件
- 抓取__doPostBack数据时如何维护session?
- 文本数据扫描比较替换算法,请教思路!
- 打包数据库不成功!
- 请问Sql Server 2005 Express支持Distributed Transaction吗?
- winform的背景图片怎么随窗体大小变化
- OracleClient问题
- 大家觉得c#怎么样?我看了一下,居然连指针都不能用,tnnd,这算什么?
- 我在后台去查找RDLC中的节点,总是找不到,求解。
- 用gembox保存excel老是出现can`t creat file ...is used by another process
cmd.Parameters.Add(new SqlParameter("Age", age));
因为这两句写在循环内,所以会重复添加参数
The variable name '@N' has already been declared. Variable names must be unique within a query batch or stored procedure.cmd.CommandText = "insert into Peoples(Name,Age) values(@N,@A)";
cmd.Parameters.Add(new SqlParameter("N", name));
cmd.Parameters.Add(new SqlParameter("A", age));
所以我就觉得好像 visual studio 给我报的错不是正确的!
cmd.Parameters.Add(new SqlParameter("@Age", age));
//public SqlParameter Add(string parameterName, SqlDbType sqlDbType);
//添加参数
cmd.CommandText = "insert into Peoples(Name,Age) values(@Name,@Age)";
cmd.Parameters.Add("@Name", SqlDbType.nvarchar,50);
cmd.Parameters["@Name"].Value = name;
cmd.ExecuteNonQuery();是否没设置参数类型~~
新手~~~勿喷~~~~
不是不是,是我的 database 已经建立好了字段 和类型,不用 指定类型了。
好奇怪,我如果把 我的文件中该生 123 、avc 等英文的形式是没有问题的,但是我不懂的就是,我表中的Name 字段在定义的时候是 nvarchar(50),类型了,应该支持中文的,为什么不行就不懂了。
cmd.Parameters.Clear()
cmd.CommandText = "insert into Peoples(Name,Age) values(@Name,@Age)";
现在新的问题,就是我的问题如果是中文,我读到表中,显示的都是空格。
但是我的表中的Name字段是 nvarchar 类型,我就不懂为什么了!
我去看一下visual studio 的编码看看,怎么看!