使用C#插入记录到MySQL数据库 [英] Inserting record in to MySQL Database using C#

查看:955
本文介绍了使用C#插入记录到MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发使用C#WPF应用程序。我想数据存储到MySQL数据库。下面是我的代码

I am currently developing an application using C# WPF. I am trying to store data into a MySQL Database. Below is the code that I have.

 MySqlCommand cmd = new MySqlCommand("", conn);

                cmd.CommandText = "INSERT INTO BUG_REPORTS (bug_softwareID, bug_firstName, bug_lastName, bug_email, bug_description, bug_ip_addr, bug_dateReported) "
                    + "VALUES (@softwareID, @firstName, @lastName, @email, @description, @ip_addr, @dateReported)";

                cmd.Parameters.Add("@softwareID");
                cmd.Parameters.Add("@firstName");
                cmd.Parameters.Add("@lastName");
                cmd.Parameters.Add("@email");
                cmd.Parameters.Add("@description");
                cmd.Parameters.Add("@ip_addr");
                cmd.Parameters.Add("@dateReported");

                cmd.Parameters["@softwareID"].Value = softwareID;
                cmd.Parameters["@firstName"].Value = getFirstName();
                cmd.Parameters["@lastName"].Value = getLastName();
                cmd.Parameters["@email"].Value = getEmail();
                cmd.Parameters["@description"].Value = getDescription();
                cmd.Parameters["@ip_addr"].Value = ip_addr;
                cmd.Parameters["@dateReported"].Value = date;

                cmd.ExecuteNonQuery();



每次我尝试插入它的错误出现创纪录的只有了MySQLParameter对象可以存储。我在做什么错。我发现的文章,一切似乎是好的。

Everytime I try to insert a record it comes up with the error 'Only MySQLParameter objects may be stored. What am I doing wrong. I found article and everything appears to be OK.

感谢您的帮助,您可以提供

Thanks for any help you can provide

推荐答案

在您paramters尝试:

In your paramters try:

cmd.Parameters.Add(new OdbcParameter("@softwareID", softwareID));



等等,等等你的参数的其余部分。

And so on and so forth with the rest of your parameters.

和在所有诚实它可能是一样简单只是不带参数建立你的SQL内联和执行命令,除非你没有验证文字和关心注入攻击。

And in all honesty it might be just as simple just to build your sql inline and execute the command without parameters unless your not validating text and are concerned about injection attacks.

这篇关于使用C#插入记录到MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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