如何在C#.Net Windows应用程序项目中使用参数化查询在Mysql表中插入? [英] How Do I Insert In Mysql Table Using Parameterized Query In C#.Net Windows Application Project?

查看:62
本文介绍了如何在C#.Net Windows应用程序项目中使用参数化查询在Mysql表中插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c#.net windows应用程序项目中使用参数化查询在Mysql表中插入?

i已经使用了下面这些给出的代码但是每次都插入表中的NULL值行。请帮助!!!



how do i insert in Mysql table using parameterized query in c#.net windows application project?
i have used these given below codes but in table NULL value rows inserted every time.please help!!!

//C# Code for windows Application
string sql = "insert into nationality(NAME)values(@NAME)";
OdbcCommand cmd = new OdbcCommand(sql,con);
cmd.Parameters.AddWithValue("@NAME", "ABC");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
//C# Code for windows Application







SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `nationality`
-- ----------------------------
DROP TABLE IF EXISTS `nationality`;
CREATE TABLE `nationality` (
  `NAME` varchar(255) DEFAULT NULL,
  `CODE` int(11) DEFAULT NULL,
  PRIMARY KEY (`CODE`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of nationality
-- ----------------------------

推荐答案

我不确定这是否会修复null -row-issue但它肯定是你的代码的一个问题:



在你的CREATE TABLE语句中,你已经将CODE声明为一个整数列,但你指定了一个String-值(123)到相应的参数。如果您删除了引号(123)但可能无法正常工作,因为您将CODE定义为 AUTO_INCREMENT [ ^ ]。您可能根本不想在INSERT语句中指定CODE,因此MySQL将自动为其分配值。 (或者,如果您想自己指定值,请从列定义中删除AUTO_INCREMENT关键字。)



编辑:这个答案指的是问题的早期版本
I'm not sure if this will fix the null-row-issue but it's definitely one issue of your code:

In your CREATE TABLE-statement you've declared CODE as an integer column but you assign a String-value ("123") to the according parameter. It would probably work if you removed the quotes (123) but not work like you intended since you defined CODE also as AUTO_INCREMENT[^]. You probably don't want to specify CODE in your INSERT-statement at all so it will be assigned a value by MySQL automatically. (Or, if you do want to specify the values yourself, remove the AUTO_INCREMENT-keyword from the column-definition.)

edit: this answer refers to an earlier version of the question


这篇关于如何在C#.Net Windows应用程序项目中使用参数化查询在Mysql表中插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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