哪错了。各位大侠帮一下
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.IO;
using System.Configuration;
using System.Data.SqlClient;
namespace 手机号码归属地
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnImport_Click(object sender, EventArgs e)
{
FolderBrowserDialog dlg = new FolderBrowserDialog();
if (dlg.ShowDialog() != DialogResult.OK)
{
return;
}
string path = dlg.SelectedPath;
string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "delete from number";
cmd.ExecuteNonQuery();
conn.Close();
string[] files=Directory.GetFiles(path,"*.txt",SearchOption.AllDirectories);
SqlCommand cmd1 = new SqlCommand();
conn.Open();
cmd1.Connection = conn;
cmd1.CommandText = "insert into number(StaNO,EnON,Name) values (@StaNO,@EnON,@Name) ";
foreach (string file in files)
{
string 运营商 = Path.GetFileNameWithoutExtension(file);
string[] lines = File.ReadAllLines(file, Encoding.Default);
foreach (string line in lines)
{
string[] strs = line.Split('-');
string 开始号码 = strs[0];
string 结束号码 = strs[1];
string 姓名 = strs[2];
cmd1.Parameters.Clear();
cmd1.Parameters.Add(new SqlParameter("StaON", 开始号码));
cmd1.Parameters.Add(new SqlParameter("EndON", 结束号码));
cmd1.Parameters.Add(new SqlParameter("Name", 运营商 + 姓名));
cmd1.ExecuteNonQuery();----------- 提示:必须声明标量变量 "@StaNO"。
}
cmd1.Clone();
cmd1.Dispose();
}
MessageBox.Show("数据导入成功");
}
}
}
------解决方案--------------------cmd1.CommandText = "insert into number(StaNO,EnON,Name) values (@StaNO,@EnON,@Name) ";
cmd1.Parameters.Add(new SqlParameter("StaON", 开始号码));
------解决方案--------------------cmd1.Parameters.Add(new SqlParameter("@StaON", 开始号码));
cmd1.Parameters.Add(new SqlParameter("@EndON", 结束号码));
cmd1.Parameters.Add(new SqlParameter("@Name", 运营商 + 姓名));
------解决方案--------------------
cmd1.CommandText = "insert into number(StaNO,EnON,Name) values (@StaNO,@EnON,@Name) ";
cmd1.Parameters.Add(new SqlParameter("StaON", 开始号码));
改为
cmd1.Parameters.Add(new SqlParameter("StaNO", 开始号码));