资源预览内容
第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
亲,该文档总共6页全部预览完了,如果喜欢就下载吧!
资源描述
c#连接连接 SQL 数据库的使用方法数据库的使用方法最佳答案 1.提取单条记录 /using System.Data; /using System.Data.SqlClient;using (SqlConnection cn = new SqlConnection(%1) /ConfigurationManager.ConnectionStrings“db2ConnectionString“.Connectio nString /Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;SqlCommand cmd = new SqlCommand(“Select Count(*) From jobs“,cn);cn.Open(); %2=cmd.ExecuteScalar(); /Message.InnerHtml2.单值比较 /using System.Data; /using System.Data.SqlClient; using (SqlConnection cn = new SqlConnection(%1) /ConfigurationManager.ConnectionStrings“db2ConnectionString“.Connectio nString /Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;SqlCommand cmd = new SqlCommand(“Select Count(*) From jobs“,cn);cn.Open(); %2=cmd.ExecuteScalar(); /Message.InnerHtml if(%2=%3) %4 3.显示表格 /using System.Data; /using System.Data.SqlClient; DataSet ds=null; using (SqlConnection cn = new SqlConnection(%1)/ConfigurationManager.ConnectionStrings“db2ConnectionString“.Connectio nString /Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;cn.Open(); cmd=new SqlDataAdapter(“Select * From Author“,cn); ds=new DataSet(); cmd.Fill(ds,%2); /“作者“ MyDataGrid.DataSource=ds.Tables(%2).DefaultView; /“作者“ MyDataGrid.DataBind();4.操作表格 /using System.Data; /using System.Data.SqlClient; using (SqlConnection cn = new SqlConnection(%1) /ConfigurationManager.ConnectionStrings“db2ConnectionString“.Connectio nString /Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;cn.Open(); cmd=new SqlDataAdapter(“Select * From Author“,cn); ds=new DataSet(); cmd.Fill(ds,%2); /“作者“ MyDataGrid.DataSource=ds.Tables(%2).DefaultView; /“作者“ MyDataGrid.DataBind();5.数值范围查询 /using System.Data; /using System.Data.SqlClient; using (SqlConnection cn = new SqlConnection(%1) /ConfigurationManager.ConnectionStrings“db2ConnectionString“.Connectio nString /Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;6.关闭时断开连接/using System.Data; /using System.Data.SqlClient; using (SqlConnection cn = new SqlConnection(%1) /ConfigurationManager.ConnectionStrings“db2ConnectionString“.Connectio nString /Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;7.执行命令 /using System.Data; /using System.Data.SqlClient; using (SqlConnection cn = new SqlConnection(%1) /ConfigurationManager.ConnectionStrings“db2ConnectionString“.Connectio nString /Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;SqlCommand cmd = new SqlCommand(“insert userRegister_t values(“ + TextBox1.Text + “,“ + TextBox2.Text + “)“,cn);cn.Open();cmd.ExecuteNonQuery();7.Oracle8/8i/9i 数据库(thin 模式) /using System.Data; Oracle Set Your custom connection strings values ODBC New version Driver=Microsoft ODBC for Oracle;Server=myServerAddress;Uid=myUsername;Pwd=myPassword; Old version Driver=Microsoft ODBC Driver for Oracle;ConnectString=OracleServer.world;Uid=myUsername;Pwd=myPassw ord; OLE DB, OleDbConnection (.NET) Standard security This connection string uses a provider from Microsoft. Provider=msdaora;Data Source=MyOracleDB;User Id=myUsername;Password=myPassword; Standard Security This connection string uses a provider from Oracle. Provider=OraOLEDB.Oracle;Data Source=MyOracleDB;User Id=myUsername;Password=myPassword; Trusted Connection Provider=OraOLEDB.Oracle;Data Source=MyOracleDB;OSAuthent=1; OracleConnection (.NET) Standard Data Source=MyOracleDB;Integrated Security=yes; This one works only with Oracle 8i release 3 or later Specifying username and password Data Source=MyOracleDB;User Id=myUsername;Password=myPassword;Integrated Security=no; This one works only with Oracle 8i release 3 or later Missing the System.Data.OracleClient namespace? Download .NET Managed Provider for Oracle Great article! “Features of Oracle Data Provider for .NET“ by Rama Mohan G. at C# Corner Omiting tnsnames.ora This is another type of Oracle connection string that doesnt rely on you to have a DSN for the connection. You create a connection string based on the format used in the tnsnames.ora file without the need to actually have one of these files on the client pc. SERVER=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost) (PORT=MyPort)(CONNECT_DATA=(SERVICE_NAME=MyOracleSID);uid= myUsername;pwd=myPassword; Core Labs OraDirect (.NET) Standard User ID=myUsername;Password=myPassword;Host=ora;Pooling=true;Min Pool Size=0;Max Pool Size=100;Connection Lifetime=0; Read more at Core Lab And at the product page Data Shape MS Data Shape Provider=MSDataShape.1;Persist Security Info=False;Data Provider=MSDAORA;Data Source=orac;User Id=myUsername;Password=myPassword; 16.程序计时DateTime runTime = Convert.ToDateTime(System.Configuratio
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号