1

我想开发可以与所有类型的数据库(Oracle、MySQL、SQLite)一起使用的通用应用程序。所以我从一个共同的地方为每个表单提供了不同的数据源。

而且我还想使用可以接受多个参数的参数化查询。我无法使用 DbParameter 传递多个参数,它适用于单参数查询,但不适用于许多参数。

所以请帮帮我...

            dbconnect dc = new dbconnect();//class which contain data source name
            DbConnection con;
            DbCommand cmd;
            DbDataReader dr;
            DbProviderFactory df;
            DataAdapter da;

            df = DbProviderFactories.GetFactory(dbconnect.dbprovider);
            con = df.CreateConnection();
            con.ConnectionString = dbconnect.sqlstr;
            con.Open();
            cmd = df.CreateCommand();
            cmd.CommandText = "update customer set" + " name='@name'," + "address='@address'," + "phone='@phone' " + "where code='@code';";
            cmd.Connection = con;
            DbDataAdapter daa = df.CreateDataAdapter();
            daa.UpdateCommand = cmd;

            DbParameter param = df.CreateParameter();

            param.ParameterName = "@name";
            param.Value = txtname.Text;

            param.ParameterName = "@address";
            param.Value = txtadd.Text;

            param.ParameterName = "@phone";
            param.Value = txtphone.Text;

            param.ParameterName = "@code";
            param.Value = txtcode.Text;
            daa.UpdateCommand.Parameters.Add(param);
            daa.UpdateCommand.ExecuteNonQuery();

            **But this not working.** 
4

3 回答 3

4

这段代码是问题所在:

DbParameter param = df.CreateParameter();

param.ParameterName = "@name";
param.Value = txtname.Text;

param.ParameterName = "@address";
param.Value = txtadd.Text;

param.ParameterName = "@phone";
param.Value = txtphone.Text;

param.ParameterName = "@code";
param.Value = txtcode.Text;
daa.UpdateCommand.Parameters.Add(param);

您正在创建一个参数对象,多次更改其ParameterName和属性,然后将该单个参数添加到命令中。这将为您留下一个带有单个参数的命令,其名称和值将是您设置的最后一个参数。Value

相反,您想添加几个单独的参数,因此您需要Parameters.Add多次调用。例如:

DbParameter nameParam = df.CreateParameter();
nameParam.ParameterName = "@name";
nameParam.Value = txtname.Text;
daa.UpdateCommand.Parameters.Add(nameParam);

DbParameter addressParam = df.CreateParameter();
addressParam.ParameterName = "@address";
addressParam.Value = txtadd.Text;
daa.UpdateCommand.Parameters.Add(addressParam);
// etc

通过提取一个方法来添加具有给定名称和值的参数,这可以变得更加简洁:

AddParameter(daa.UpdateCommand, "@name", txtname.Text);
AddParameter(daa.UpdateCommand, "@address", txtadd.Text);
// etc
于 2012-07-17T13:10:58.763 回答
2

您只创建一个参数

        DbParameter param = df.CreateParameter();

        param.ParameterName = "@name";
        param.Value = txtname.Text;

ETC...

您需要 DbParameter为每个创建一个新的

        DbParameter param = df.CreateParameter();
        param.ParameterName = "@name";
        param.Value = txtname.Text;
        daa.UpdateCommand.Parameters.Add(param);

        param = df.CreateParameter();
        param.ParameterName = "@address";
        param.Value = txtadd.Text;
        daa.UpdateCommand.Parameters.Add(param);

ETC....

并将每个添加到命令中。

于 2012-07-17T13:10:42.377 回答
0

DbDataAdapter 通常用于批量或多行更新。对于单行更新,请使用 DbCommand。

DbProviderFactory df = DbProviderFactories.GetFactory(dbconnect.dbprovider);             
            using(DbConnection con = df.CreateConnection())
            {
                con.ConnectionString = "dbconnect.sqlstr";              
                DbCommand cmd = df.CreateCommand(); 
                cmd.CommandText = "update customer set" + " name='@name'," + "address='@address'," + "phone='@phone' " + "where code='@code';";
                cmd.Connection = con; 

                DbParameter param1 = df.CreateParameter(); 
                param1.ParameterName = "@name"; 
                param1.Value = txtname.Text;

                DbParameter param2 = df.CreateParameter(); 
                param2.ParameterName = "@address"; 
                param2.Value = txtadd.Text; 

                DbParameter param3 = df.CreateParameter(); 
                param3.ParameterName = "@phone"; 
                param3.Value = txtphone.Text; 

                DbParameter param4 = df.CreateParameter(); 
                param4.ParameterName = "@code"; 
                param4.Value = txtcode.Text;

                con.Open();
                cmd.ExecuteNonQuery();
            }
于 2012-07-17T13:51:42.077 回答