带有mysql的Dapper dot net,无论是Fatal还是全部插入为NULL

时间:2012-08-28 10:55:21

标签: c# mysql dapper

我的代码

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作为bool并且在db中是tinyint(1)而在另一种情况下所有都是字符串

如果我添加“允许用户变量=真;”然后我没有得到异常,但在数据库中所有值都为null(检查他得到值)

在这个项目中使用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);

仍在寻求帮助,因为我想使用小巧玲珑....

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

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
        });
}

主要更改是使用查询&lt;&gt;扩展vs执行。执行没有返回结果,我看到你想要一些计数回来,例如“int cnt = mycon.Execute(query,”。

我还将查询包装到方法中;现在您的客户端只需要发送一个新的ProxyUser来插入。这样您还可以控制数据类型。

希望这会有所帮助。 祝你好运

答案 1 :(得分:0)

尝试用@替换: 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);";