我创建了注册表,但是数据插入到注册表中并没有保存在表中,而且erron来了“不正确的语法@STATE或@EDUCATION ETC”。 [英] i create registration form but data insert into registration form are not save in table and erron came "INCORRECT SYNTAX NEAR @STATE OR @EDUCATION ETC"

查看:70
本文介绍了我创建了注册表,但是数据插入到注册表中并没有保存在表中,而且erron来了“不正确的语法@STATE或@EDUCATION ETC”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Data;
使用 System.Data.SqlClient;
使用 System.Configuration;

public class Datalayer
{

DataSet ds;
SqlDataAdapter da;
SqlCommand cmd;
SqlConnection con;

public Datalayer()
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings [ conn]。ConnectionString);
}

public void inserdata(propertylayer p)
{
cmd = new SqlCommand( 插入信息值(@ name,@ address,@ gender,@ hobby,@ city,@ education,con);
cmd.Parameters.AddWithValue( @ name,p.name);
cmd.Parameters.AddWithValue( @ address,p.address);
cmd.Parameters.AddWithValue( @ gender,p.gender);
cmd.Parameters.AddWithValue( @ 业余爱好,p.hobby);
cmd.Parameters.AddWithValue( @ city,p.city);
cmd.Parameters.AddWithValue( @ education,p.education);
// cmd.Parameters.AddWithValue(@state,p.state);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}

}

解决方案

你忘了关闭括号:

 cmd =  new  SqlCommand( 插入信息值(@ name,@ address,@ gender,@ hobby,@ city,@ education,con); 

应该是:

 cmd =  new  SqlCommand( 插入信息值(@ name,@ address,@ gender,@ hobby,@ city,@ education),con); 





但是,在INSERT语句中列出字段是一个更好的想法,因为代码随后变得更加独立于SQL表设计 - 您的代码希望列定义在一个指定的顺序 - 在开头插入一个新字段可能会导致代码崩溃:

 cmd = new SqlCommand( 插入信息(姓名,地址,性别,业余爱好,城市,教育)价值观(@姓名,@地址,@性别,@爱好,@城市,@教育),con); 


如果这是您正在执行的代码的真实副本......那么问题是,您忘记关闭insert语句中的括号。 ..



- 声明应该是:



cmd =新的SqlCommand(插入信息值 (@ name,@ address,@ gender,@ hobby,@ city,@ education);,con);



- 别忘了提名字在您的查询中table_name之后的列,如果您没有为表中存在的每个列输入值...



谢谢,希望这会有所帮助..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public class Datalayer
{

    DataSet ds;
    SqlDataAdapter da;
    SqlCommand cmd;
    SqlConnection con;
    
	public Datalayer()
	{
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
	}

    public void inserdata(propertylayer p)
    {
        cmd = new SqlCommand("insert into info values(@name,@address,@gender,@hobby,@city,@education", con);
        cmd.Parameters.AddWithValue(@"name", p.name);
        cmd.Parameters.AddWithValue(@"address", p.address);
        cmd.Parameters.AddWithValue(@"gender", p.gender);
        cmd.Parameters.AddWithValue(@"hobby", p.hobby);
        cmd.Parameters.AddWithValue(@"city", p.city);
        cmd.Parameters.AddWithValue(@"education", p.education);
        //cmd.Parameters.AddWithValue(@"state", p.state);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }

}

解决方案

You forgot a close bracket:

cmd = new SqlCommand("insert into info values(@name,@address,@gender,@hobby,@city,@education", con);

Should be:

cmd = new SqlCommand("insert into info values(@name,@address,@gender,@hobby,@city,@education)", con);



But it is a much, much better idea to list the fields in the INSERT statement as the code then becomes more independent of the SQL table design - your code expects the columns to be defined in a specified order - the insertion of a new field at the beginning could crash your code:

cmd = new SqlCommand("INSERT INTO info (Name, Address, Gender, Hobby, City, Education) VALUES (@name,@address,@gender,@hobby,@city,@education)", con);


If this is the real copy of the code that you are executing...then the problem is that, you forgot to close the bracket in your "insert" statement...

- The statement should be :

cmd = new SqlCommand("insert into info values(@name,@address,@gender,@hobby,@city,@education);", con);

- Don't forget to mention the name of columns after table_name in your query, in-case you are not entering values for each column that exist in the table...

Thanks and hope this helps..


这篇关于我创建了注册表,但是数据插入到注册表中并没有保存在表中,而且erron来了“不正确的语法@STATE或@EDUCATION ETC”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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