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错误,请问哪里错了??

解决方案 »

  1.   

    trystring tjcomm = "insert into [table](imei) values ('" + strLine + "')";
      

  2.   

    是不是strLine 的长度超过了字段imei定义的长度了
    另外如楼上所说,table是关键字,需要加[table]
      

  3.   

    各位大大
    IMEI字段,我设置了唯一,就是不能重复,我就是想写入重复的话,就把内容显示出来,然后接着执行循环。
      

  4.   

    现在可以写数据库了,但是IMEI字段不能重复,所以我设置了唯一,我就想导入数据的时候如果报重复错误,把重复的内容显示出来,然后接着导剩下的数据。
      

  5.   

    List<string>list=new List<string>();
    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就好了
      

  6.   

    wuzhengqing1大大:提示continue;没有要中断或继续的封闭循环
      

  7.   

    hjywyj大大: 
    我去掉continue,报错跳出来之后就不循环。zuxiadulibno大大:
    表名我已经改了
    我加continue就提示:没有要中断或继续的封闭循环 
    不加,报错就跳出循环了
      

  8.   

    加上try,catch,你这个没有继续。除了异常直接结束了。
    在while循环里加。
      

  9.   

    谢谢!现在的问题好像变成循环了,而且richTextBox1.AppendText(strLine + "\n");
    都是一个串码   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);            }
                
            }
      

  10.   

    catch (Exception)
            {            
                //list.Add(L+1+","+strLine);
     
                richTextBox1.AppendText(strLine + "\n");
                L++;
    strLine = m_streamReader.ReadLine();
                continue;
            }
    你得让它继续去读啊。
      

  11.   

    01.string 表达式 = "INSERT INTO 数据表名 (字段名1,字段名2) VALUES ( ";  
    02.       表达式 += "'" + 数据1 + "', '" + 数据2 + "')";  
    03.       AccEss数据库增删改(表达式, false);  
    AccEss数据库增删改