.net web Service 连接数据库
在.net中发布连接数据库的web service.
step 1: Add referrence: System.web, System.web.services
使用名字空间: using System.Data.SqlClient;
using System.Web.Configuration;
using System.Configuration;
step 2: create web.config to set the data base connection string
<configuration>
<connectionStrings>
<add name="WSConStringSQL" connectionString="Data Source=wenzhiyan;AttachDbFilename=|DataDirectory|\MatLibTest.mdf; uid =sa ; pwd = 811208; Integrated Security=False;User Instance=False" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
step 3: Web service中读取该connection string.
//~/web.config和app_data在同一目录下面,不知是不是必须这样
Configuration WebConfig = WebConfigurationManager.OpenWebConfiguration("~/web.config");
//获得connection string
ConnectionStringSettings ConStr = WebConfig.ConnectionStrings.ConnectionStrings["WSConStringSQL"];
SqlConnection SqlCon = new SqlConnection(ConStr.ConnectionString);
SqlCommand SqlCom = new SqlCommand("Select * from manufacturer where name = @name", SqlCon);
SqlCom.Parameters.Add("@name", SqlDbType.NVarChar);
SqlCom.Parameters["@name"].Value = "Hello";
SqlCon.Open();
posted on 2007-01-29 10:28 像大海一样...... 阅读(432) 评论(0) 收藏 举报
浙公网安备 33010602011771号