数据库表user如图
                代码如下:
this.Open();
string cmdString="INSERT INTO user(ID,Frist,Second,Third,Fourth,Fifth,Sixth,Blue,Name) VALUES    ('3000001','1','2','3','4','5','6','10','name')";
SqlCommand cmd=new SqlCommand(cmdString,con);
cmd.ExecuteNonQuery();
this.Close();
现运行如下如下错误:数据库已能连接上,求问解决办法。

解决方案 »

  1.   

    图片内容看不到,最好贴错误代码上来
    定义con来连接数据库,把this换成con试试
      

  2.   

    图片内容看不到,blue是整形吗?
      

  3.   

    大哥,怎么可能用this呢....
    SqlConnection conn = new SqlConnection("连接数据库字符串");
    conn.open();
    string sqlStr = "T-SQL语句"
    SqlCommand cmd = new SqlCommand(sqlStr,conn);
    cmd.ExecuteNonQuery();
    conn.close();
      

  4.   

    this属于关键字,不要随便用的啊
      

  5.   

    LZ应该学习下如何在这里发图片this.open,this,close中不能用this.不用this如果还有问题那才叫怪呢?
      

  6.   

    整形也支持 '30001' 这种写法, 应该不是数据类型的问题先排除: ID 不能重复的错误再改成下面试试:
    con.Open();
    string cmdString="INSERT INTO user(ID,Frist,Second,Third,Fourth,Fifth,Sixth,Blue,Name) VALUES ('3000001','1','2','3','4','5','6','10','name')";
    SqlCommand cmd=new SqlCommand(cmdString,con);
    cmd.ExecuteNonQuery();
    con.Close();
      

  7.   

    this是因为前面有Open()方法
    ]#region OPEN
    public void Open()
    {
    if(con==null)
    {
    string connectionString="Server=Localhost;database=DB_Ticket;uid=sa;pwd=123";
        con=new SqlConnection(connectionString);
    }
    if(con.State==System.Data.ConnectionState.Closed)
    con.Open();
    Console.WriteLine("数据库状态为"+con.State.ToString());
    }
    #endregion
    Close()方法
    #region CLOSE
    public void Close()
    {
    if(con!=null)
    {
    con.Close();
    }
    }
    #endregion ,数据库连接是没有问题的,con.State.ToString()为Open,在写入数据库的时候报错。ID Varchar(50)类型,‘30000001’这种数据类型不对?报错代码如下:调试器抛出异常System.Data.SqlClient.SqlException:
    关键字 'user' 附近有语法错误。RunExecuteNonQueryTds()
    InternalExecuteNonQuery()
    ExecuteNonQuery()
      

  8.   

    能把你的拼好的sql语句放到查询分析器中执行一下么?
      

  9.   

    你看看 cmdString 生成的是什么值
      

  10.   

    解决了,原因如下:表user在SQL中是关键字,改了个表名,或者用【user】能够解决。呵呵,谢谢大家,开始散分了。