简体   繁体   English

将数据存储在sql server 2005 asp.net c#网站中

[英]store data in sql server 2005 asp.net c# website

I have created a project which has internal/in built VS database... now I want to connect it to external SQL server database 我创建了一个具有内部/内置VS数据库的项目...现在我想将其连接到外部SQL Server数据库

How can I do this? 我怎样才能做到这一点? Does it affect my project? 这会影响我的项目吗? How about connection string that I created during my inbuilt database? 在内置数据库中创建的连接字符串如何?

You can connect to external database by specifying its name and credentials.If it is on your pc than in server give name of your PC.And if you are using sql server authentication than you have to give username and password 您可以通过指定外部数据库的名称和凭据来连接到外部数据库。如果它在您的PC上而不是服务器上,请提供您的PC的名称;如果您使用sql服务器身份验证,则必须提供用户名和密码

server=yourpcname;database=databasename;uid=userid;pwd=***;Pooling=false;

If you are using windown authentication 如果您正在使用windown身份验证

server=yourpcname;database=databasename;Pooling=false;

Add to web.config in 添加到web.config中

configuration\\connectionStrings 配置\\连接字符串

<add name="SOMECONNECTIONNAME" connectionString="server=SOMEIPORNAME;database=SOMEDATABASENAME;user id=SOMESQLUSER;password=SOMEPASSWORD" providerName="System.Data.SqlClient"/>

Call in code by 调用代码

SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SOMECONNECTIONNAME"]);
            connection.Open();
SqlCommand command = new SqlCommand("StoreProcedurename", connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@DateFrom", DBNull.Value);
                command.Parameters.AddWithValue("@DateTo", DBNull.Value);
                SqlDataReader DR = command.ExecuteReader();

etc. 等等

@Pramod Niralakeri @Pramod Niralakeri

Connect to External database using connection string. 使用连接字符串连接到外部数据库。

first add the connection string to web configuration file 首先将连接字符串添加到Web配置文件

      <connectionStrings>
      <add name="managementportal" connectionString="Data Source=HSBHV;Initial Catalog=itamma00_HSB;User ID=sa;Password=HSB555" />
    </connectionStrings>

then use the ConfigurationManager in cs page using connection name "managementportal" 然后在cs页面中使用连接名称“ managementportal”使用ConfigurationManager

      SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["managementportal"].ConnectionString);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM