我的代码
string query = @"INSERT INTO representative_dev.representative_users
(Username, Password, FullName,
HomePhone, PhoneToCall,
CompanyEmail, PersonalEmail,
IsManager)
VALUES (@Username, @Password, @FullName,
@HomePhone, @PhoneToCall,
@CompanyEmail, @PersonalEmail,
@IsManager);";
mycon.Open();
int cnt = mycon.Execute(query,
new
{
txtUsername,
txtPassword,
txtFullName,
txtHomePhone,
txtPhoneToCall,
txtCompanyEmail,
txtPersonalEmail,
cbxIsManager
});
mycon.Close();
它给了我"执行命令时遇到致命错误。"
当我在另一种情况下执行另一个插入查询时,它工作得很好,不同的是,这里我将cbxIsManager作为布尔,在db中是tinyint(1),在另一个情况下,所有都是字符串
如果我添加"允许用户变量=true;",那么我不会得到异常,但在数据库中,所有值都为空(检查他是否得到值)
在这个项目中使用ASP.NET,在另一个项目中使用控制台,都是FW4,mysql是6.somthing(想想3)另一个代码
string query = @"INSERT INTO representative_dev.potencial_contact
(Name, Company_ID, Phone, Email, Department, Notes, SuperFriend, UpdateDate)
VALUES (@Name, @Company_ID, @Phone, @Email, @Department, @Notes, @SuperFriend, @UpdateDate);";
mycon.Open();
int cnt = mycon.Execute(query,
new
{
con.Name,
con.Company_ID,
con.Phone,
con.Email,
con.Department,
con.Notes,
con.SuperFriend,
con.UpdateDate
});
mycon.Close();
如有任何帮助,将不胜感激
thx,ariel
编辑:什么时候起作用的
string query = @"INSERT INTO representative_dev.representative_users
(Username, Password, FullName,
HomePhone, PhoneToCall,
CompanyEmail, PersonalEmail,
IsManager)
VALUES ('" + txtUsername + @"', '" + txtPassword + @"', '" + txtFullName + @"',
'" + txtHomePhone + @"', '" + txtPhoneToCall + @"',
'" + txtCompanyEmail + @"', '" + txtPersonalEmail + @"',
" + cbxIsManager + ");";
int cnt = mysql.ExecuteInsert(query);
仍在寻求帮助,因为我想使用dapper。。。。
试试这样的东西:
public class RepresentativeUser
{
public string UserName { get; set; }
public string Pasword { get; set; }
//etc
public int IsManager { get; set; }
}
public void InsertRepresentativeUser(RepresentativeUser representativeUser)
{
const string query = @"INSERT INTO representative_dev.representative_users
(Username, Password,
IsManager)
VALUES (@Username, @Password,
@IsManager);
select count(*) from representative_dev.representative_users";
var count = DbConnection.Query<decimal>(query,
new
{
representativeUser.UserName,
representativeUser.Pasword,
representativeUser.IsManager
});
}
主要更改是使用Query<>扩展与执行。Execute不返回结果,我看到您希望返回一些计数,例如"int cnt=mycon.Execute(query,".
我还将查询封装到一个方法中;现在,您的客户端只需要发送一个新的RepresentativeUser即可插入。通过这种方式,您还可以控制数据类型。
希望这能有所帮助。祝好运
尝试用:
s替换@
s。它似乎对我有效:
string query = @"INSERT INTO representative_dev.representative_users
(Username, Password, FullName, HomePhone, PhoneToCall, CompanyEmail,
PersonalEmail, IsManager) VALUES (:Username, :Password, :FullName,
:HomePhone, :PhoneToCall, :CompanyEmail, :PersonalEmail, :IsManager);";