2023年6月21日发(作者:)

参数详解 这个问题以前总是遇到,但是每次都是找到解决⽅法就没有具体找原因。昨天再次遇到参数问题,才想着⼀定要搞清楚了。 中不同数据提供者所⽤参数格式如下: Provider Named/Positional Parameter Marker SqlClient Named @parmname OracleClient Named parmname (or parmname) OleDb Positional ? Odbc Positional ? 在dType=时,DbParameter就要符合上述要求。如 SqlParameter则要使⽤参数名来决定参数值,⽽oledb则是根据参数顺序来决定参数值。但是当dType= rocedure时,则可以采⽤采⽤占位符或名字来确定参数值。 这种不统⼀使得要写出跨越各种数据提供者的程序变得复杂了。同时要实现统⼀的数据层当然还要考虑不同数据库的sql语句区别(所以要尽量采⽤标准的sql 语句)。本来提供了很好的类结构(DbConnection, DbCommand等以Db开头的类)来实现这种很重要的功能,可是这些原因使得我们还是要⾃⼰写不少代码。 下⾯是⼀段⽰例代码: //create proc TestParam //@courseid varchar(50) //as //select * from course where courseid=@courseid using System; using c; using ; using ; using ent; using ; namespace ADOParameterTest { class Program { enum ConnectionType : byte { Sql, Ole }; static string sqlConnectionString = "Data Source=CTEC-LLY;Initial catalog=examdb;Integrated Security=True"; static string oleConnectionString = "Provider=sqloledb;Data Source=CTEC-LLY;Initial Catalog=examdb;User Id=sa;Password=sa"; static DbConnection GetConnection(ConnectionType ct){ if (ct == )

{ return new SqlConnection(sqlConnectionString); } else { return new OleDbConnection(oleConnectionString); } } static void TestOleCommandText() { ine("TestOleCommandText"); using(DbConnection con = GetConnection()) { (); DbCommand cmd = Command(); //dText = "declare @courseid as varchar(50); set @courseid=’0001’;select * from course where courseid=@courseid"; dText = "select * from course where courseid=?"; //odbc&oledb只能是?做占位符,这时跟参数名⽆关跟参数顺序是相关的 DbParameter p = Parameter(); terName = "courseid"; = "0001"; (p); //下⾯被注释的参数设置⽅式也是对的,可以注释上⾯四⾏⽽采⽤下⾯的语句 //记住参数名是没有意义的,顺序决定参数 //(new OleDbParameter("@courseid", "0001")); ShowResult(eReader()); } } static void TestOleStoredProcedure()

{ ine("TestOleStoredProcedure"); using (DbConnection con = GetConnection()) { (); DbCommand cmd = Command(); dType = Procedure; dText = "TestParam"; ine("⽤@param做占位符"); //odbc&oledb只能是?做占位符,这时跟参数名⽆关跟参数顺序是相关的 DbParameter p = Parameter(); terName = "@courseid"; = "0001"; (p); //下⾯被注释的参数设置⽅式也是对的,可以注释上⾯四⾏⽽采⽤下⾯的语句 //记住参数名是没有意义的,顺序决定参数 //(new OleDbParameter("@courseid", "0001")); ShowResult(eReader()); ine("⽤?做占位符"); terName = ""; ShowResult(eReader()); } }

static void TestSqlCommandText() { ine("TestSqlCommandText"); using (DbConnection con = GetConnection()) {

(); DbCommand cmd = Command(); //sql只能是@param做占位符,跟oledb相反,跟参数名有关跟参数顺序⽆关 //oracle只能⽤:param做占位符,跟参数名有关跟参数顺序⽆关 dText = "select * from course where courseid=@courseid"; DbParameter p = Parameter(); terName = "@courseid"; = "0001"; (p); //下⾯被注释的参数设置⽅式也是对的,可以注释上⾯四⾏⽽采⽤下⾯的语句 //记住只有参数名有意义,顺序⽆关 //(new OleDbParameter("@courseid", "0001")); ShowResult(eReader()); } } static void TestSqlStoredProcedure() { ine("TestSqlStoredProcedure"); using (DbConnection con = GetConnection()) { (); DbCommand cmd = Command(); //sql只能是@param做占位符,跟oledb相反,跟参数名有关跟参数顺序⽆关 //oracle只能⽤:param做占位符,跟参数名有关跟参数顺序⽆关 dType = Procedure; dText = "TestParam"; ine("⽤@param做占位符"); DbParameter p = Parameter(); terName = "@courseid"; = "0001"; (p); //下⾯被注释的参数设置⽅式也是对的,可以注释上⾯四⾏⽽采⽤下⾯的语句 //记住只有参数名有意义,顺序⽆关 //(new OleDbParameter("@courseid", "0001")); ShowResult(eReader()); ine("⽤?做占位符"); terName = ""; ShowResult(eReader()); } } static void ShowResult(DbDataReader reader) { int count = ount; while(()) { ine("-------------------------------"); for(int i=0; i

2023年6月21日发(作者:)

参数详解 这个问题以前总是遇到,但是每次都是找到解决⽅法就没有具体找原因。昨天再次遇到参数问题,才想着⼀定要搞清楚了。 中不同数据提供者所⽤参数格式如下: Provider Named/Positional Parameter Marker SqlClient Named @parmname OracleClient Named parmname (or parmname) OleDb Positional ? Odbc Positional ? 在dType=时,DbParameter就要符合上述要求。如 SqlParameter则要使⽤参数名来决定参数值,⽽oledb则是根据参数顺序来决定参数值。但是当dType= rocedure时,则可以采⽤采⽤占位符或名字来确定参数值。 这种不统⼀使得要写出跨越各种数据提供者的程序变得复杂了。同时要实现统⼀的数据层当然还要考虑不同数据库的sql语句区别(所以要尽量采⽤标准的sql 语句)。本来提供了很好的类结构(DbConnection, DbCommand等以Db开头的类)来实现这种很重要的功能,可是这些原因使得我们还是要⾃⼰写不少代码。 下⾯是⼀段⽰例代码: //create proc TestParam //@courseid varchar(50) //as //select * from course where courseid=@courseid using System; using c; using ; using ; using ent; using ; namespace ADOParameterTest { class Program { enum ConnectionType : byte { Sql, Ole }; static string sqlConnectionString = "Data Source=CTEC-LLY;Initial catalog=examdb;Integrated Security=True"; static string oleConnectionString = "Provider=sqloledb;Data Source=CTEC-LLY;Initial Catalog=examdb;User Id=sa;Password=sa"; static DbConnection GetConnection(ConnectionType ct){ if (ct == )

{ return new SqlConnection(sqlConnectionString); } else { return new OleDbConnection(oleConnectionString); } } static void TestOleCommandText() { ine("TestOleCommandText"); using(DbConnection con = GetConnection()) { (); DbCommand cmd = Command(); //dText = "declare @courseid as varchar(50); set @courseid=’0001’;select * from course where courseid=@courseid"; dText = "select * from course where courseid=?"; //odbc&oledb只能是?做占位符,这时跟参数名⽆关跟参数顺序是相关的 DbParameter p = Parameter(); terName = "courseid"; = "0001"; (p); //下⾯被注释的参数设置⽅式也是对的,可以注释上⾯四⾏⽽采⽤下⾯的语句 //记住参数名是没有意义的,顺序决定参数 //(new OleDbParameter("@courseid", "0001")); ShowResult(eReader()); } } static void TestOleStoredProcedure()

{ ine("TestOleStoredProcedure"); using (DbConnection con = GetConnection()) { (); DbCommand cmd = Command(); dType = Procedure; dText = "TestParam"; ine("⽤@param做占位符"); //odbc&oledb只能是?做占位符,这时跟参数名⽆关跟参数顺序是相关的 DbParameter p = Parameter(); terName = "@courseid"; = "0001"; (p); //下⾯被注释的参数设置⽅式也是对的,可以注释上⾯四⾏⽽采⽤下⾯的语句 //记住参数名是没有意义的,顺序决定参数 //(new OleDbParameter("@courseid", "0001")); ShowResult(eReader()); ine("⽤?做占位符"); terName = ""; ShowResult(eReader()); } }

static void TestSqlCommandText() { ine("TestSqlCommandText"); using (DbConnection con = GetConnection()) {

(); DbCommand cmd = Command(); //sql只能是@param做占位符,跟oledb相反,跟参数名有关跟参数顺序⽆关 //oracle只能⽤:param做占位符,跟参数名有关跟参数顺序⽆关 dText = "select * from course where courseid=@courseid"; DbParameter p = Parameter(); terName = "@courseid"; = "0001"; (p); //下⾯被注释的参数设置⽅式也是对的,可以注释上⾯四⾏⽽采⽤下⾯的语句 //记住只有参数名有意义,顺序⽆关 //(new OleDbParameter("@courseid", "0001")); ShowResult(eReader()); } } static void TestSqlStoredProcedure() { ine("TestSqlStoredProcedure"); using (DbConnection con = GetConnection()) { (); DbCommand cmd = Command(); //sql只能是@param做占位符,跟oledb相反,跟参数名有关跟参数顺序⽆关 //oracle只能⽤:param做占位符,跟参数名有关跟参数顺序⽆关 dType = Procedure; dText = "TestParam"; ine("⽤@param做占位符"); DbParameter p = Parameter(); terName = "@courseid"; = "0001"; (p); //下⾯被注释的参数设置⽅式也是对的,可以注释上⾯四⾏⽽采⽤下⾯的语句 //记住只有参数名有意义,顺序⽆关 //(new OleDbParameter("@courseid", "0001")); ShowResult(eReader()); ine("⽤?做占位符"); terName = ""; ShowResult(eReader()); } } static void ShowResult(DbDataReader reader) { int count = ount; while(()) { ine("-------------------------------"); for(int i=0; i