当我尝试使用MSSQL在asp.net中启动Web项目时,似乎无法连接到数据库。错误消息是:
A network related or instance specific error occurred while establishing a connection to SQL server...
但是,当我尝试运行以前的项目时,它可以工作。但是,当我选择所有程序-> Microsoft SQL Server 2008->配置工具-> SQL Server配置管理器-> SQL Server服务时。它没有显示所有服务,而是向我显示了一个错误,即远程过程调用失败。
我不知道如何解决此问题,因为它仅发生在我的一个项目中。提前致谢。
我的web.config代码:
编辑:
<?xml version="1.0" encoding="UTF-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="GeospatialChallenge2014.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web>
<webServices>
<protocols>
<add name="HttpSoap" />
<add name="HttpGet" />
<add name="HttpPost" />
</protocols>
</webServices>
<compilation debug="true" targetFramework="4.0">
</compilation>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" />
</system.web>
<connectionStrings>
<add name="GeospatialChallengeConnectionString" connectionString="Data Source=(local);Initial Catalog=SAFETY_AT_SG_DATABASE.mdf;"
providerName="System.Data.SqlClient" />
</connectionStrings>
<applicationSettings>
<GeospatialChallenge2014.Properties.Settings>
<setting name="GeospatialChallenge2014"
serializeAs="String">
<value>http://localhost/SgDataService.asmx</value>
</setting>
</GeospatialChallenge2014.Properties.Settings>
</applicationSettings>
<system.webServer>
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>
和我的连接字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml;
namespace Geospatial_Challenge_2014
{
/// <summary>
/// Summary description for SgDataService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class SgDataService : System.Web.Services.WebService
{
[WebMethod]
public DataSet GetTrafficByDateTime(string StartDateTime, string EndDateTime)
{
//System.Data.SqlClient.SqlConnection myConn = new System.Data.SqlClient.SqlConnection();
//myConn.ConnectionString = @"Data Source=(local);Initial Catalog=SAFETY_AT_SG_DATABASE.MDF;Integrated Security=True";
SqlConnection myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["GeospatialChallengeConnectionString"].ConnectionString);
SqlDataAdapter myAdapter = new SqlDataAdapter("Select * from dbo.Traffic where Convert(DateTime,([date]+' '+[time])) >= @StartDateTime AND Convert(DateTime,([date]+' '+[time])) <= @EndDateTime;", myConn);
myAdapter.SelectCommand.Parameters.AddWithValue("@StartDateTime", StartDateTime);
myAdapter.SelectCommand.Parameters.AddWithValue("@EndDateTime", EndDateTime);
DataSet ds = new DataSet();
myConn.Open();
myAdapter.Fill(ds, "Traffic");
myConn.Close();
return ds;
}
}
}
杜德(Dude)在web.config本身内定义连接字符串,然后尝试。
<connectionStrings>
<add name="DBConnection" connectionString="Data Source=yourservername;Initial
Catalog=DBname;User ID=sa;Password=yourpassword"
providerName="System.Data.SqlClient" />
</connectionStrings>
并在您的方法中调用连接字符串为
SqlConnection objConnection = new SqlConnection();//sql connection object
objConnection.ConnectionString = ConfigurationManager.ConnectionStrings
["DBConnection"].ConnectionString; //conn string name as in config file
SqlCommand cmd = new SqlCommand("YourStoredprocedure", objConnection);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句