1

我的代码

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 作为布尔值,而在数据库中是 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();

任何帮助,将不胜感激

谢谢,爱丽儿

编辑:这是什么时候起作用的

            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....

4

2 回答 2

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

主要变化是使用 Query<> 扩展 vs Execute。执行不返回结果,我看到您希望返回一些计数,例如“int cnt = mycon.Execute(query”)。

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

希望这可以帮助。祝你好运

于 2012-08-28T16:37:48.440 回答
0

尝试用@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);";
于 2016-01-07T23:40:27.957 回答