我的SQL插入格式有什么问题,c#没有执行它?

时间:2017-09-01 18:13:52

标签: c# mysql sql

 var config = require(__dirname + '/../config/config.json')[env];
 // your config file will be in your directory
 var sequelize = new Sequelize(config.database, config.username, config.password, {
    host: '****.****.us-west-1.rds.amazonaws.com',
    port: 5432,
    logging: console.log,
    maxConcurrentQueries: 100,
    dialect: 'postgres',
    dialectOptions: {
        ssl:'Amazon RDS'
    },
    pool: { maxConnections: 5, maxIdleTime: 30},
    language: 'en'
})
  

创建表时发生异常:关键字附近的语法不正确   '插入&#39 ;.   ','附近的语法不正确。 System.Data.SqlClient.SqlException   按任意键继续 。 。

我完全难以理解为什么当我尝试运行我的程序时,我得到了一个这个例外。整个程序只是将某人注册到一个带有独特票证的数据库中。附在它上面。我可以收集数据没问题,并且创建了与我的数据库的连接,但是我的insert语句出现语法错误。

2 个答案:

答案 0 :(得分:2)

以下是我将如何重写此内容以遵循一些更好的做法,并希望同时解决您的语法问题:

// leave your datatypes as-is, no conversions
string first = user1.FirstName;
string last = user1.LastName;
string ticket = user1.Ticket;
int age = user1.Age;

// property dispose of your sql connection and commands; 
// they implement IDisposable so you should be using a using

using (var conn = new SqlConnection("Data source=(local); Database=TicketingVoting;User Id=****;Password=****"))
{
    try
    {
        conn.Open();


        SqlCommand insertdata = new SqlCommand(null, conn);

        insertdata.CommandText = "Insert into [TicketingVoting].dbo.Registration1 (FirstName,LastName,Age,Ticket) VALUES (@fname, @lname, @age, @ticket);"

        insertdata.Parameters.Add("@fname", SqlDbType.VarChar, 30).Value = first;
        insertdata.Parameters.Add("@lname", SqlDbType.VarChar, 30).Value = last;
        insertdata.Parameters.Add("@age", SqlDbType.Int).Value = age;
        insertdata.Parameters.Add("@ticket", SqlDbType.VarChar, 30).Value = ticket;

        insertdata.Prepare(); // prepare the statement
        insertdata.ExecuteNonQuery();

        Console.WriteLine("Inserting Data Successfully");
    }
    catch (Exception e)
    {
        Console.WriteLine("Exception Occur while creating table:" + e.Message + "\t" + e.GetType());
    }
}

您必须将参数类型和字符长度调整为它们在数据库中的实际值。

答案 1 :(得分:1)

这是您的实际代码吗?

string command = Convert.ToString(("Insert into [TicketingVoting].dbo.Registration1 (FirstName,LastName,Age,Ticket) VALUES ('{0}','{1}','{2}','{3}');", first, last, age, ticket));

对我而言,结果

  
    

方法没有超载' ToString'需要5个参数

  
string first = "Fname";
string last = "LName";
int age = 65;
int ticket = 45687;
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Insert into [TicketingVoting].dbo.Registration1 (FirstName,LastName,Age,Ticket) VALUES ('{0}','{1}','{2}','{3}');", first, last, age, ticket);
string command = sb.ToString();

//Using Convert.ToString() like your original code
string commandTwo = Convert.ToString(new StringBuilder().AppendFormat("Insert into [TicketingVoting].dbo.Registration1 (FirstName,LastName,Age,Ticket) VALUES ('{0}','{1}','{2}','{3}');", first, last, age, ticket));

//using string.Format()
string commandThree = string.Format("Insert into [TicketingVoting].dbo.Registration1 (FirstName,LastName,Age,Ticket) VALUES ('{0}','{1}','{2}','{3}');", first, last, age, ticket);