小弟初学C#,写了一个聊天软件,要把聊天记录保存到ACCESS。用ACCESS2003建了一个名为db1.mdb的数据库,其中有一个名为table1的表,表里有一个名为record的记录放聊天记录。运行后可以把发送的文字记录下来,但接受的记录不下来,老报错,如下:
System.Data.OleDb.OleDbException: 字符串的语法错误 在查询表达式 ''  (2006-5-19 19:54:41)  77' 中。
   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr)
   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
   at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
   at p2pChat.MainForm.saverecord(String msg) in c:\documents and settings\liujia\my documents\毕设\p2pchat\p2pchat\mainform.cs:line 277
小弟肯请各位高人指点迷津
源代码如下:

解决方案 »

  1.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.IO;
    using System.Net.Sockets;
    using System.Threading;
    using System.Data.OleDb;namespace p2pChat
    {
    /// <summary>
    /// MainForm 的摘要说明。
    /// </summary>
    public class MainForm : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox txtIp;
    private System.Windows.Forms.TextBox txtRecord;
    private System.Windows.Forms.TextBox txtName;
    private System.Windows.Forms.Button btnSend;
    private System.Windows.Forms.TextBox txtContent;
    private TcpListener tcpLister = new TcpListener(5566);
    System.Threading.ThreadStart listenPort;
    System.Threading.Thread lister;
    private System.Windows.Forms.Label label3;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public MainForm()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //不显示最大化按钮
    this.MaximizeBox = false; listenPort += new ThreadStart(this.Listen);
    lister = new Thread(listenPort);
    this.Closing += new System.ComponentModel.CancelEventHandler(abortLister);
    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.label1 = new System.Windows.Forms.Label();
    this.txtIp = new System.Windows.Forms.TextBox();
    this.txtRecord = new System.Windows.Forms.TextBox();
    this.label2 = new System.Windows.Forms.Label();
    this.txtName = new System.Windows.Forms.TextBox();
    this.btnSend = new System.Windows.Forms.Button();
    this.txtContent = new System.Windows.Forms.TextBox();
    this.label3 = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(16, 232);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(72, 23);
    this.label1.TabIndex = 0;
    this.label1.Text = "目标地址:";
    this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    // 
    // txtIp
    // 
    this.txtIp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
    this.txtIp.Location = new System.Drawing.Point(80, 232);
    this.txtIp.Name = "txtIp";
    this.txtIp.Size = new System.Drawing.Size(200, 21);
    this.txtIp.TabIndex = 1;
    this.txtIp.Text = "";
    // 
    // txtRecord
    // 
    this.txtRecord.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
    this.txtRecord.Location = new System.Drawing.Point(16, 32);
    this.txtRecord.Multiline = true;
    this.txtRecord.Name = "txtRecord";
    this.txtRecord.ReadOnly = true;
    this.txtRecord.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
    this.txtRecord.Size = new System.Drawing.Size(264, 176);
    this.txtRecord.TabIndex = 4;
    this.txtRecord.Text = "";
    // 
    // label2
    // 
    this.label2.Location = new System.Drawing.Point(24, 256);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(48, 23);
    this.label2.TabIndex = 5;
    this.label2.Text = "呢 称:";
    this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    // 
    // txtName
    // 
    this.txtName.Location = new System.Drawing.Point(80, 256);
    this.txtName.Name = "txtName";
    this.txtName.Size = new System.Drawing.Size(88, 21);
    this.txtName.TabIndex = 6;
    this.txtName.Text = "";
    // 
    // btnSend
    // 
    this.btnSend.Location = new System.Drawing.Point(200, 256);
    this.btnSend.Name = "btnSend";
    this.btnSend.Size = new System.Drawing.Size(64, 23);
    this.btnSend.TabIndex = 0;
    this.btnSend.Text = "发 送";
    this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
    // 
    // txtContent
    // 
    this.txtContent.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
    | System.Windows.Forms.AnchorStyles.Right)));
    this.txtContent.Location = new System.Drawing.Point(16, 288);
    this.txtContent.Multiline = true;
    this.txtContent.Name = "txtContent";
    this.txtContent.Size = new System.Drawing.Size(264, 152);
    this.txtContent.TabIndex = 8;
    this.txtContent.Text = "";
    // 
    // label3
    // 
    this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
    | System.Windows.Forms.AnchorStyles.Right)));
    this.label3.Location = new System.Drawing.Point(16, 8);
    this.label3.Name = "label3";
    this.label3.Size = new System.Drawing.Size(100, 16);
    this.label3.TabIndex = 9;
    this.label3.Text = "聊天记录:";
    this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
    // 
    // MainForm
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 453);
    this.Controls.Add(this.label3);
    this.Controls.Add(this.txtContent);
    this.Controls.Add(this.btnSend);
    this.Controls.Add(this.txtName);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.txtRecord);
    this.Controls.Add(this.txtIp);
    this.Controls.Add(this.label1);
    this.Name = "MainForm";
    this.Text = "局域网聊天程序";
    this.Load += new System.EventHandler(this.MainForm_Load);
    this.ResumeLayout(false); }
    #endregion
      

  2.   

    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new MainForm());
    } //开始监听
    private void Listen()
    {
    try
    {
    tcpLister.Start();
    while(true)
    {
    Socket s = tcpLister.AcceptSocket();
    Byte[] stream = new Byte[80];
    int i = s.Receive(stream);
    string message = System.Text.Encoding.UTF8.GetString(stream);
    this.txtRecord.AppendText(message);
    saverecord(message);
    }
    }
    catch(System.Security.SecurityException)
    {
    MessageBox.Show("防火墙安全错误!","错误",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
    }
    catch(System.Exception)
    {
    // this.txtRecord.AppendText("已停止监听!");
    }
    }

    private void abortLister(object sender,System.ComponentModel.CancelEventArgs e)
    {
    this.tcpLister.Stop();
    } //发送
    private void btnSend_Click(object sender, System.EventArgs e)
    {
    if(this.txtContent.Text=="")
    {
    MessageBox.Show("不能发送空信息!","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
    }
    else
    {
    this.Send();
    }
    } //发送消息
    private void Send()
    {
    try
    {
    string msg = this.txtName.Text+"  ("+System.DateTime.Now.ToString()+")\r\n"+this.txtContent.Text+"\r\n";
    TcpClient client = new TcpClient(this.txtIp.Text,5566);
    NetworkStream sendStream = client.GetStream();
    StreamWriter writer = new StreamWriter(sendStream);
    writer.Write(msg);
    writer.Flush();
    sendStream.Close();
    client.Close();
    this.txtRecord.AppendText(msg);
    this.txtContent.Clear();
    saverecord("send"+msg);
    }
    catch(System.Exception)
    {
    this.txtRecord.AppendText("目标计算机拒绝连接请求!\r\n");
    }
    } private void MainForm_Load(object sender, System.EventArgs e)
    {
    this.txtRecord.AppendText("正在监听...\r\n");
    lister.Name = "监听本地端口";
    lister.Start();
    } public static void saverecord ( string msg ) 

    try 

    string message1 = msg.Replace("\r"," ");
    message1 = message1.Replace("\n"," ");
    string strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db1.mdb " ; 
    OleDbConnection myConn = new OleDbConnection ( strConn ) ; 
    myConn.Open ( ) ; 
    string strInsert = "INSERT INTO table1 (record) VALUES ('"+message1+"')";
    OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ; 
    inst.ExecuteNonQuery ( ) ; 
    myConn.Close ( ) ; 

    catch ( Exception ed ) 

    MessageBox.Show ( "保存数据记录发生 " + ed.ToString ( ) , "错误!" ) ; 
    StreamWriter fwriter = File.AppendText("error.txt");
    fwriter.WriteLine(ed.ToString ( ));
    fwriter.Close();


    }
    }
      

  3.   

    change 
    string strInsert = "INSERT INTO table1 (record) VALUES ('"+message1+"')";
    with
    string strInsert = "INSERT INTO table1 ([record]) VALUES ('"+message1+"')";
      

  4.   

    技术交流群号:23266021
    欢迎大家在此讨论关于.net的各种技术。1号群
      

  5.   

    好像是SQL的语法错误,确切的说是日期自动有错误,在日期字段的两边要加入#号,
    如:select * from tablename where dateField=# 日期 #
    你试一下是不是这个问题
      

  6.   

    谢谢楼上各位的回帖,但问题还是存在。
    我再详细的说明一下问题:比如我给127.0.0.1发信息,一模一样的文字发送的时候可以记录到数据库,接受时就报上面的错误,既然发送的信息可以记录下来我想应该不是SQL的语法错误,况且我的record字段只是一个文本字段,日期我也是按文本存在这个字段里的。我发的信息里也没有用单引号。无论在哪台电脑接受什么样的信息都会报这个错,我真的无奈了:(
      

  7.   

    你接收到的message1有问题,拼接sql后sql有语法错误
      

  8.   

    .net技术群 1号群:23266021,此群已满!
    2号群已经开放:4510861
    请大家赶快加入!
      

  9.   

    应该是sql拼串的问题,你单步调试,把品串完成后的连接串局部变量考到文本编辑器,检查一下是否有问题。