.net mysql 参数_.net c# Mysql参数问题

I'm trying to write a query to my database. When I originally wrote it I didn't use parameters, I am now trying to do so But I am getting a Mysql Error now.

Column count doesn't match value at row1

Which in my 1st column I have an auto Incremented number labeled as "ID" Which I do not believe I have to add to the query because of the auto increment..

This is the newly written code:

MySqlConnection conn = new MySqlConnection(dbConnection);

string Query = "INSERT INTO tasklist.tasks (TaskName, AssignedTo, StartDate, EndDate, Description, DirPath, Completed) VALUES (?TaskName),(?AssignedTo),(?StartDate),(?EndDate),(?Description),(?DirPath),(?Completed);";

MySqlCommand insertCommand = new MySqlCommand(Query, conn);

conn.Open();

insertCommand.Parameters.AddWithValue("?TaskName", txtTaskName.Text);

insertCommand.Parameters.AddWithValue("?AssignedTo", txtAssignTo.Text);

insertCommand.Parameters.AddWithValue("?StartDate", txtdateStart.Value.ToString("yyyy-M-d"));

insertCommand.Parameters.AddWithValue("?EndDate", txtDateEnd.Value.ToString("yyyy-M-d"));

insertCommand.Parameters.AddWithValue("?Description", txtTaskDescription.Text);

insertCommand.Parameters.AddWithValue("?DirPath", txtDirPath.Text);

insertCommand.Parameters.AddWithValue("?Completed",("0"));

insertCommand.ExecuteNonQuery();

conn.Close();

Remember the previous way without parameters worked... however with parameters does not work. What exactly am I doing wrong here? Any help would be greatly appreciated.

Talk2:

When using .add VS states that .add is deprecated and to use .AddWithValue

Talk3:

Look more closely - it is the .Add(object, object) form that is deprecated. Parameters.Add("@p1", MySqlDbType.Foo).Value = varBar; Specifying the datatype prevents any misunderstanding or unwanted type conversions

Talk4:

I see, thanks for the info

Solutions1

Use only one pair of parentheses around all parameters

INSERT INTO tasklist.tasks (TaskName, AssignedTo, StartDate, EndDate, Description, DirPath, Completed)

VALUES (?TaskName, ?AssignedTo, ?StartDate, ?EndDate, ?Description, ?DirPath, ?Completed)

Talk1:

, note that all the values per row you're inserting are within () parentheses, not each value. If you were inserting 2 rows, the second set of values would also be contained, together, in another set.

Talk2:

Yeah, I just figured it out right before I came back to check. This is what I did. string Query = "INSERT INTO tasklist.tasks (TaskName, AssignedTo, StartDate, EndDate, Description, DirPath, Completed) VALUES ((?TaskName), (?AssignedTo), (?StartDate), (?EndDate), (?Description), (?DirPath), (?Completed));"; which fixed it

Talk3:

Sorry, I code a lot in SQF for A game called Arma 3. Redundant parentheses are common and needed in the language... Force of habit I guess. Will fix. Thanks for the constructive criticism.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值