我的代码
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();
它给了我 “执行命令时遇到 fatal error 。”
当我在另一种情况下执行另一个插入查询时效果很好,不同的是这里我将 cbxIsManager 作为 bool 而在数据库中是 tinyint(1) 而在另一种情况下都是字符串
如果我添加“Allow User Variables=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();
任何帮助将不胜感激
谢谢,爱丽儿
编辑: 这是什么时候起作用的
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<> 扩展 vs Execute。执行不返回结果,我看到你想要返回一些计数,例如“int cnt = mycon.Execute(查询,”。
我还将查询包装到一个方法中;现在你的客户只需要发送一个新的 RepresentativeUser 来插入。这样您还可以控制数据类型。
希望这对您有所帮助。 祝你好运
关于c# - Dapper dot net with mysql,Fatal 或 insert all as NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12157777/