今天真是郁闷极了,花了几个小时才搞定一个小问题,一个SQL语句,在ACCESS可以实现插入功能,但放在程序却报INSERT INTO语句的语法错误,一直搞不懂,在调试,还是不行。郁闷坏了,啥话都不说了,以后得注意了:代码如下:
原码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
namespace TxtToDB
{ public partial class Form1 : Form
{ public Form1()
{
InitializeComponent();
}
public class Selefields
{
/// <summary>
/// id
/// </summary>
public string OPERATOR_CODE;
/// <summary>
/// PWD
/// </summary>
public string PASSWORD;
/// <summary>
/// name
/// </summary>
public string OPERATOR_NAME;
/// <summary>
/// card
/// </summary>
public string STAFF_CARD_NUMBER;
/// <summary>
/// op type
/// </summary>
public string OPERATOR_TYPE;
/// <summary>
/// station
/// </summary>
public string STATION_CODE;
/// <summary>
/// ticket
/// </summary>
public string POSITION_CODE;
/// <summary>
/// op1
/// </summary>
public string OPERATOR_STATE;
/// <summary>
/// op2
/// </summary>
public string OPERATOR_STATUS;
/// <summary>
/// dtc
/// </summary>
public string CREATE_DATE;
/// <summary>
/// dmc
/// </summary>
public string CREATE_TIME;
/// <summary>
/// stt
/// </summary>
public string START_DATE;
/// <summary>
/// ett
/// </summary>
public string END_DATE;
/// <summary>
/// youxiao
/// </summary>
public string PASSWORD_END_DATE;
}
private void button1_Click(object sender, EventArgs e)
{
ReadAndInsert();
this.dataGridView1.DataSource = GetData("select * from [Local_BOMlogin]");
}
private void ReadAndInsert()
{
string basePath = Application.StartupPath + @"\..\..\myfiles\";
string fileName = "oper_info.txt";
FileStream fs = File.Open(basePath + fileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
string strRead;
string mysql;
bool flag = true;
while (flag)
{
if ((strRead = sr.ReadLine()) == null)
{
flag = false;
}
else
{
string[] sArray = strRead.Split('|');
Selefields down = new Selefields();
down.OPERATOR_CODE = sArray[0];
////dr = dv.NewRow();
string a = sArray[0];
string b = sArray[1];
string c = sArray[2];
string d = sArray[3];
string e = sArray[4];
string f = sArray[5];
string g = sArray[6];
string h = sArray[7];
string i = sArray[8];
string j = sArray[9];
string k = sArray[10];
string l = sArray[11];
string m = sArray[12];
string n = sArray[13];
string my = "insert into [Local_BOMlogin] ([OPERATOR_CODE],[PASSWORD],[OPERATOR_NAME],[STAFF_CARD_NUMBER],[OPERATOR_TYPE],[STATION_CODE],[POSITION_CODE],[OPERATOR_STATE],[OPERATOR_STATUS],[CREATE_DATE],[CREATE_TIME],[START_DATE],[END_DATE],[PASSWORD_END_DATE]) values('" + a.Trim() + "','" + b.Trim() + "','" + c.Trim() + "','" + d.Trim() + "','" + e.Trim() + "','" + f.Trim() + "','" + g.Trim() + "','" + h.Trim() + "','" + i.Trim() + "','" + j.Trim() + "','" + k.Trim() + "','" + l.Trim() + "','" + m.Trim() + "','" + n.Trim() + "')";
//string my = "insert into names(nameid,username,sex) values('2','3','2')";
OleDbConnection conn = getConn();
OleDbCommand mycmd = new OleDbCommand(my, conn);
try
{
conn.Open();
mycmd.ExecuteNonQuery();
//MessageBox.Show("sucess");
}
catch (Exception ex)
{
throw (new Exception(ex.Message));
}
finally
{
mycmd.Dispose();
conn.Close();
}
}
}
fs.Dispose();
sr.Dispose();
}
public DataView GetData(string GetSql)
{
DataView dataview;
System.Data.DataSet dataset = new DataSet();
try
{
OleDbConnection conn = getConn();
OleDbDataAdapter adp = new OleDbDataAdapter();
adp.SelectCommand = new OleDbCommand(GetSql, conn);
adp.Fill(dataset, "mytable");
conn.Close();
}
catch (Exception ee)
{
MessageBox.Show("Error: \n\n" + ee.Message);
}
dataview = new DataView(dataset.Tables["mytable"]);
return dataview;
}
public bool UpdateData(string UpdateSql)
{
OleDbConnection conn = getConn();
OleDbCommand mycmd = new OleDbCommand(UpdateSql, conn);
try
{
conn.Open();
mycmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
throw (new Exception(ex.Message));
}
finally
{
mycmd.Dispose();
conn.Close();
}
}
public OleDbConnection getConn()
{
try
{
string connstr = "Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=" + Application.StartupPath + @"\..\..\myfiles\BOM_log.mdb";
OleDbConnection tempconn = new OleDbConnection(connstr);
return (tempconn);
}
catch (Exception ex)
{
MessageBox.Show("Error:\n\n" + ex.Message);
return null;
}
}
}
}