尝试添加到数据库时出现未处理的错误? OdbcParameterCollection只接受非null的OdbcParameter类型对象,而不接受String对象。
protected void Button1_Click(object sender, EventArgs e)
{
string email = TextBox1.Text; //added variable name
string firstname = TextBox2.Text;
string secondname = TextBox3.Text;
string dob = TextBox4.Text;
string location = TextBox5.Text;
string aboutme = TextBox6.Text;
OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite; User=root; Password=;");
cn.Open();
OdbcCommand cmd = new OdbcCommand("INSERT INTO User (Email, FirstName, SecondName, DOB, Location, Aboutme) VALUES (email, firstname, secondname, dob, location, aboutme)"); // fixed incomplete insert statement.
cmd.Parameters.Add(email);
cmd.Parameters.Add(firstname);
cmd.Parameters.Add(secondname);
cmd.Parameters.Add(dob);
cmd.Parameters.Add(location);
cmd.Parameters.Add(aboutme);//add the parameter to the command
cmd.ExecuteNonQuery(); //actually run the sql
}
}
我尝试了这个但是我收到错误System.InvalidOperationException:ExecuteNonQuery:Connection属性尚未初始化。
我也改变了数据库,所以它不是NN(非空),看它是否有帮助,但没有快乐
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Odbc;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string email = TextBox1.Text; //added variable name
string firstname = TextBox2.Text;
string secondname = TextBox3.Text;
string dob = TextBox4.Text;
string location = TextBox5.Text;
string aboutme = TextBox6.Text;
OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite; User=root; Password=;");
cn.Open();
OdbcCommand cmd = new OdbcCommand("INSERT INTO User(Email) VALUES('?Email')");
cmd.Parameters.Add(new OdbcParameter("?email", email));
cmd.ExecuteNonQuery();
}
}
数据库中的数据是大写字母,如'L'ocation,我的字符串设置为小写。不确定它是否在我的代码中回到了前面?
-- -----------------------------------------------------
-- Table `gymwebsite`.`User`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `gymwebsite`.`User` (
`UserID` INT NOT NULL AUTO_INCREMENT ,
`Email` VARCHAR(245) NULL ,
`FirstName` VARCHAR(45) NULL ,
`SecondName` VARCHAR(45) NULL ,
`DOB` VARCHAR(15) NULL ,
`Location` VARCHAR(45) NULL ,
`Aboutme` VARCHAR(245) NULL ,
PRIMARY KEY (`UserID`) )
ENGINE = InnoDB;
INSERT INTO `gymwebsite`.`user`
(`UserID`,
`Email`,
`FirstName`,
`SecondName`,
`DOB`,
`Location`,
`Aboutme`)
VALUES
(
{UserID: INT},
{Email: VARCHAR},
{FirstName: VARCHAR},
{SecondName: VARCHAR},
{DOB: VARCHAR},
{Location: VARCHAR},
{Aboutme: VARCHAR}
);
EDIT `gymwebsite`.`user`;
答案 0 :(得分:1)
使用此
OdbcCommand cmd = new OdbcCommand("INSERT INTO User (Email, FirstName, SecondName, DOB, Location, Aboutme) VALUES ("+ email +","+ firstname+","+SecondName+","+DOB+","+Location+","+Aboutme+")",cn);
尝试使用我之前发布的参数...并使其正常工作。
问候
答案 1 :(得分:0)
指定此行的所有参数 -
var v1 = cmd.Parameters.Add("email",OdbcType.VarChar, , 30);
v1.Value = email;
答案 2 :(得分:0)
您需要将OdbcParameter
个实例添加到参数集合中:
cmd.Parameters.Add(new OdbcParameter("email", email));
...
第一个参数是insert语句中参数的名称,第二个参数是用于确定DbType
和内容的对象。
答案 3 :(得分:0)
您可能还需要切换SQL命令以使用如下问号:
OdbcCommand cmd = new OdbcCommand("INSERT INTO User (Email, FirstName, SecondName, DOB, Location, Aboutme) VALUES (?, ?, ?, ?, ?, ?)");
答案 4 :(得分:0)
你试过这个吗?
cmd.Parameters.Add(new OdbcParameter(email));
也有一些重载。
答案 5 :(得分:0)
使用 Parameters.AddWithValue()方法,
OdbcCommand cmd = new OdbcCommand("INSERT INTO `User`
(Email, FirstName, SecondName, DOB, Location, Aboutme)
VALUES
('?Email', '?FirstName', '?SecondName', '?DOB', '?Location','?Aboutme')",cn);
cmd.Parameters.AddWithValue("?Email", textBox1.Text);
...