ORA-01036: 非法的变量名/编号
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.Data.SqlClient;
using System.Data.OracleClient;
namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (ofdfile.ShowDialog() == DialogResult.OK) //点击form窗体上的导入按钮
{
using (FileStream fileStream=File.OpenRead(ofdfile .FileName)) //选txt文本
{
using (StreamReader streamreader = new StreamReader(fileStream))
{
using (OracleConnection conn = new OracleConnection(@"Data Source=orcl;User ID=nszs;Password=1")) //打开数据库连接
{
conn.Open();
string lines = null;
using (OracleCommand cmd = new OracleCommand())
{
cmd.Connection = conn;
cmd.CommandText = "insert into a(a1,a2) values(@a1,@a2)";
while ((lines=streamreader.ReadLine()) != null)
{
string[] strs = lines.Split(' ');
string a1 = strs[0].ToString();
string a2 = strs[1].ToString();
//int age = Convert.ToInt32(strs[1]);
cmd.Parameters.Clear();
OracleParameter parn = new OracleParameter(":a1", a1);
cmd.Parameters.Add(parn);
OracleParameter parp = new OracleParameter(":a2", a2);
cmd.Parameters.Add(parp);
int result = cmd.ExecuteNonQuery();
//报错处
conn.Close();
cmd.Dispose();
}
}
}
}
}
MessageBox.Show("数据导入成功");
}
}
}
}
------解决方案--------------------cmd.CommandText = "insert into a(a1,a2) values(@a1,@a2)";
=>
cmd.CommandText = "insert into a(a1,a2) values(
:a1,
:a2)";