访问数据库,数据库无反应 [英] Access DB, No reaction from the DB

查看:58
本文介绍了访问数据库,数据库无反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到问题...也许是查询语法中的某些问题,并且编译器不会抛出任何异常.但是表不接收信息.

Encountered with a problem... maybe something in the syntax of the query, and the compiler doesn't throw any Exception. But the table do not receive information.

你有没有注意到可能是错误的?

Anything you noticed that maybe wrong?

OleDbConnection conn;
OleDbCommand cmd;


public Commands(OleDbConnection con)
{
    conn = con;
}


public Commands()
{
    conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DB.accdb");
}

public void node_Join(int id, byte[] mac)
{
    try
    {
        conn.Open();
        cmd = new OleDbCommand(@"INSERT INTO Nodes ([J_ID],[Node ID],[Node MAC],[Line Quality],[Status]) values('" + Convert.ToString(id) + @"',0,'" + BitConverter.ToString(mac) + @"',0,'Join')", conn);
        cmd.ExecuteNonQuery();
        conn.Close();
    }
    catch (Exception exc)
    {
        conn.Close();
        MessageBox.Show(exc.Message.ToString());
    }
}

推荐答案

不清楚你想做什么,我想冒险回答,但无论如何..

It is not clear what you try to do and I feel like taking a risk to answer but anyway..

正如我在评论中所说的,您应该始终使用 参数化查询.这种字符串连接对SQL 注入攻击开放.

As I said in my comment, you should always use parameterized queries. This kind of string concatenations are open for SQL Injection attacks.

并且由于您的 J_ID 列是 Number,因此没有必要将其插入一个带有 Convert.ToString(id) 的字符串.使用 id(我假设它是一个整数)可能没问题.

And since your J_ID column is Number, there is no point to insert it a string with Convert.ToString(id). Using id (I assume it is an integer) will probably fine.

也使用 using 声明处理您的 OleDbConnection.

试试这个;

using(OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DB.accdb"))
{
  using(cmd = new OleDbCommand(@"INSERT INTO Nodes ([J_ID],[Node ID],[Node MAC],[Line Quality],[Status]) values(?, ?, ?, ?, ?", conn))
  {
    cmd.Parameters.AddWithValue("@id", id);
    cmd.Parameters.AddWithValue("@nodeid", 0);
    cmd.Parameters.AddWithValue("@nodemac", BitConverter.ToString(mac));
    cmd.Parameters.AddWithValue("@line", 0);
    cmd.Parameters.AddWithValue("@status", "Join");
    conn.Open();
    cmd.ExecuteNonQuery();
  }
}

还要考虑史蒂夫在他的评论中的建议.

Also think Steve's suggestion in his comment.

这篇关于访问数据库,数据库无反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆