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.OleDb;
using System.IO;namespace WindowsFormsApplication1
{ public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
}
public OleDbConnection GreatConn()
{
OleDbConnection objConnection = new OleDbConnection("provider=Microsoft.jet.OLEDB.4.0;data Source=E:/VB/data.Mdb");
return objConnection;
} private void button2_Click(object sender, EventArgs e)
{
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
openFileDialog1.RestoreDirectory = true;
openFileDialog1.FilterIndex = 1;
openFileDialog1.ShowDialog();
FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open);
StreamReader m_streamReader = new StreamReader(fs);
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);// 从数据流中读取每一行,直到文件的最后一行 string m = "ip"; string arry="";
string strLine = m_streamReader.ReadLine(); OleDbConnection conn = GreatConn();
conn.Open(); while (strLine != null)
{
Console.WriteLine("{0}", strLine);
string dt=DateTime.Now.ToString();
string tjcomm = "insert into table(imei) values ('" + strLine + "')";
OleDbCommand comm = new OleDbCommand(tjcomm, conn);
comm.ExecuteNonQuery();
comm.Dispose(); richTextBox1.AppendText(strLine + "\n");
strLine = m_streamReader.ReadLine(); }
conn.Close();
m_streamReader.Close();
fs.Close();
}
}
}
提示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.OleDb;
using System.IO;namespace WindowsFormsApplication1
{ public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
}
public OleDbConnection GreatConn()
{
OleDbConnection objConnection = new OleDbConnection("provider=Microsoft.jet.OLEDB.4.0;data Source=E:/VB/data.Mdb");
return objConnection;
} private void button2_Click(object sender, EventArgs e)
{
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
openFileDialog1.RestoreDirectory = true;
openFileDialog1.FilterIndex = 1;
openFileDialog1.ShowDialog();
FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open);
StreamReader m_streamReader = new StreamReader(fs);
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);// 从数据流中读取每一行,直到文件的最后一行 string m = "ip"; string arry="";
string strLine = m_streamReader.ReadLine(); OleDbConnection conn = GreatConn();
conn.Open(); while (strLine != null)
{
Console.WriteLine("{0}", strLine);
string dt=DateTime.Now.ToString();
string tjcomm = "insert into table(imei) values ('" + strLine + "')";
OleDbCommand comm = new OleDbCommand(tjcomm, conn);
comm.ExecuteNonQuery();
comm.Dispose(); richTextBox1.AppendText(strLine + "\n");
strLine = m_streamReader.ReadLine(); }
conn.Close();
m_streamReader.Close();
fs.Close();
}
}
}提示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.OleDb;
using System.IO;namespace WindowsFormsApplication1
{ public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
}
public OleDbConnection GreatConn()
{
OleDbConnection objConnection = new OleDbConnection("provider=Microsoft.jet.OLEDB.4.0;data Source=E:/VB/data.Mdb");
return objConnection;
} private void button2_Click(object sender, EventArgs e)
{
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
openFileDialog1.RestoreDirectory = true;
openFileDialog1.FilterIndex = 1;
openFileDialog1.ShowDialog();
FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open);
StreamReader m_streamReader = new StreamReader(fs);
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);// 从数据流中读取每一行,直到文件的最后一行 string m = "ip"; string arry="";
string strLine = m_streamReader.ReadLine(); OleDbConnection conn = GreatConn();
conn.Open(); while (strLine != null)
{
Console.WriteLine("{0}", strLine);
string dt=DateTime.Now.ToString();
string tjcomm = "insert into table(imei) values ('" + strLine + "')";
OleDbCommand comm = new OleDbCommand(tjcomm, conn);
comm.ExecuteNonQuery();
comm.Dispose(); richTextBox1.AppendText(strLine + "\n");
strLine = m_streamReader.ReadLine(); }
conn.Close();
m_streamReader.Close();
fs.Close();
}
}
}
提示insert into错误,请问哪里错了??
另外如楼上所说,table是关键字,需要加[table]
IMEI字段,我设置了唯一,就是不能重复,我就是想写入重复的话,就把内容显示出来,然后接着执行循环。
try
{
while (strLine != null)
{
Console.WriteLine("{0}", strLine);
string dt = DateTime.Now.ToString();
string tjcomm = "insert into table(imei) values ('" + strLine + "')";
OleDbCommand comm = new OleDbCommand(tjcomm, conn);
comm.ExecuteNonQuery();
comm.Dispose();
richTextBox1.AppendText(strLine + "\n");
strLine = m_streamReader.ReadLine();
}
conn.Close();
m_streamReader.Close();
fs.Close();
}
catch (Exception)
{
list.add(L+1+","+strLine);
continue;
}
最后循环完了输出你的List就好了
我去掉continue,报错跳出来之后就不循环。zuxiadulibno大大:
表名我已经改了
我加continue就提示:没有要中断或继续的封闭循环
不加,报错就跳出循环了
在while循环里加。
都是一个串码 private void button2_Click(object sender, EventArgs e)
{
int L = 0;
openFileDialog1.InitialDirectory = "E:\\";
openFileDialog1.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
openFileDialog1.RestoreDirectory = true;
openFileDialog1.FilterIndex = 1;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{ FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open);
StreamReader m_streamReader = new StreamReader(fs);
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin); string strLine = m_streamReader.ReadLine(); OleDbConnection conn = GreatConn();
conn.Open(); List<string>list=new List<string>();
while (strLine != null)
{
try
{
Console.WriteLine("{0}", strLine);
string dt = DateTime.Now.ToString();
string tjcomm = "insert into [imei](imei) values ('" + strLine + "')";
OleDbCommand comm = new OleDbCommand(tjcomm, conn);
comm.ExecuteNonQuery();
comm.Dispose();
strLine = m_streamReader.ReadLine(); }
catch (Exception)
{
//list.Add(L+1+","+strLine); richTextBox1.AppendText(strLine + "\n");
L++;
continue;
}
} conn.Close();
m_streamReader.Close();
fs.Close();
string message = "导入数量总计:" + L;
MessageBox.Show(message); }
}
{
//list.Add(L+1+","+strLine);
richTextBox1.AppendText(strLine + "\n");
L++;
strLine = m_streamReader.ReadLine();
continue;
}
你得让它继续去读啊。
02. 表达式 += "'" + 数据1 + "', '" + 数据2 + "')";
03. AccEss数据库增删改(表达式, false);
AccEss数据库增删改