如何使用C#在Asp.net中设置SQL Server 2005的数据库 [英] How Set-up a dabase of sql server 2005 in Asp.net with c#

查看:64
本文介绍了如何使用C#在Asp.net中设置SQL Server 2005的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用c#在Asp.net中设置sql server 2005的数据库?

解决方案

希望您正在询问如何设置连接通过Asp.net中的C#连接到sql server 2005.如果是这样,请检查以下代码...


<pre lang="xml"><connectionStrings><br />
    <add name="OsmEmpConString" connectionString="Data Source=.\sqlexpress;Initial Catalog=AgentDB; User ID= Sqluser ; Password= Change123;"<br />
    providerName="System.Data.SqlClient" /><br />
  </connectionStrings></pre><br />



在web.config文件中创建连接并建立连接
DataSource =您的数据库源
初始目录=数据库名称
用户ID =数据库的用户ID
密码=数据库密码

在您的.cs文件中使用该连接...您可以检查以下代码

<pre lang="midl">string strDbCon = WebConfigurationManager.ConnectionStrings["OsmEmpConString"].ToString();<br />
    SqlConnection _cnAgentDB = new SqlConnection(strDbCon);<br />
       _cnAgentDB.Open();</pre><br />


_cnAgentDB是SqlConnection的实例.

在执行此操作之前,您需要在.cs文件中添加两个名称空间,
他们是

 使用 System.Web.Configuration;
使用 System.Data.SqlClient; 


sqlconnection con = new sqlconnection("connection string");
sqldataadaptor adp =新sqldataadaptor(选择查询",con);
数据集ds = new数据集();
adp.fill(ds);

现在,您已经在数据集中有了值

How Set-up a dabase of sql server 2005 in Asp.net with c#??

解决方案

Hope you are asking about how to set-up a connection to sql server 2005 through C# in Asp.net. If so Check the below code...


<pre lang="xml"><connectionStrings><br />
    <add name="OsmEmpConString" connectionString="Data Source=.\sqlexpress;Initial Catalog=AgentDB; User ID= Sqluser ; Password= Change123;"<br />
    providerName="System.Data.SqlClient" /><br />
  </connectionStrings></pre><br />



Create the connection in web.config file and establish a connection
DataSource = Your database source
Initial Catalog = Database name
User ID = User id of the database
Password = Password to the database

Use that connection in your .cs file... You can check the below code

<pre lang="midl">string strDbCon = WebConfigurationManager.ConnectionStrings["OsmEmpConString"].ToString();<br />
    SqlConnection _cnAgentDB = new SqlConnection(strDbCon);<br />
       _cnAgentDB.Open();</pre><br />


_cnAgentDB is the instance of SqlConnection.

Before going to do this, u need to add two namespaces in the .cs file,
they are

using System.Web.Configuration;
using System.Data.SqlClient;


sqlconnection con=new sqlconnection("connection string");
sqldataadaptor adp=new sqldataadaptor("Select query",con);
dataset ds=new dataset();
adp.fill(ds);

Now you have the values in dataset


这篇关于如何使用C#在Asp.net中设置SQL Server 2005的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆