问题描述
请教个问题~写一个页面,是添加一些内容到数据库中。代码如下<configuration><appSettings><addkey="dsn"value="datasource=(local);userid=sa;password=sa1234;database=library"/></appSettings><connectionStrings><addname="libraryConnectionString"connectionString="DataSource=(local);InitialCatalog=library;UserID=sa;Password=sa1234"providerName="System.Data.SqlClient"/></connectionStrings>usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.Data.Sql;usingSystem.Data.SqlClient;publicpartialclassBookIn:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){}protectedvoidButton1_Click(objectsender,EventArgse){stringstrconn=ConfigurationSettings.AppSettings["dsn"];//连接本地计算机的LMS数据库SqlConnectioncn=newSqlConnection(strconn);cn.Open();stringmysql="insertintotshxx(name,id,writer,rukudate,neirong,miaoshu)Values('"+this.TextBox1.Text+"','"+this.TextBox2.Text+"','"+this.TextBox3.Text+"','"+this.TextBox4.Text+"','"+this.TextBox5.Text+"','"+this.TextBox6.Text+"')";//创建Command对象SqlCommandcm=newSqlCommand(mysql,cn);cm.ExecuteNonQuery();cn.Close();this.Label8.Text="录入成功!";this.TextBox1.Text="";this.TextBox2.Text="";this.TextBox3.Text="";this.TextBox4.Text="";this.TextBox5.Text="";}店提交后~出现错误Anerrorhasoccurredwhileestablishingaconnectiontotheserver.WhenconnectingtoSQLServer2005,thisfailuremaybecausedbythefactthatunderthedefaultsettingsSQLServerdoesnotallowremoteconnections.(provider:NamedPipesProvider,error:40-CouldnotopenaconnectiontoSQLServer)Description:Anunhandledexceptionoccurredduringtheexecutionofthecurrentwebrequest.Pleasereviewthestacktraceformoreinformationabouttheerrorandwhereitoriginatedinthecode.ExceptionDetails:System.Data.SqlClient.SqlException:Anerrorhasoccurredwhileestablishingaconnectiontotheserver.WhenconnectingtoSQLServer2005,thisfailuremaybecausedbythefactthatunderthedefaultsettingsSQLServerdoesnotallowremoteconnections.(provider:NamedPipesProvider,error:40-CouldnotopenaconnectiontoSQLServer)SourceError:Line23://连接本地计算机的LMS数据库Line24:SqlConnectioncn=newSqlConnection(strconn);Line25:cn.Open();Line26:stringmysql="insertintotshxx(name,id,writer,rukudate,neirong,miaoshu)Values('"+this.TextBox1.Text+"','"+this.TextBox2.Text+"','"+this.TextBox3.Text+"','"+this.TextBox4.Text+"','"+this.TextBox5.Text+"','"+this.TextBox6.Text+"')";Line27://创建Command对象是什么问题~谢谢
解决方案
解决方案二:
连不到数据库。先测试你的连接字符串是否正确。
解决方案三:
怎么测试哦~谢谢诶
解决方案四:
配置文件拿出来看看
解决方案五:
晕,刚才没看见上面的,呵呵
解决方案六:
protectedstaticstringconnectionString=@"server=localhost;database=数据库名;integratedsecurity=sspi;";
解决方案七:
用con.state可以判断if(conn.state==ConnectionState.closed){conn.open();}
解决方案八:
up
解决方案九:
你先测试看看连接字符串是否正确,再在stringstrconn=ConfigurationSettings.AppSettings["dsn"];添加监视看看程序了取得连接字符串是否正确.....
解决方案十:
引用1楼yuwenge的回复:
连不到数据库。先测试你的连接字符串是否正确。
解决方案十一:
protectedstaticstringconnectionString=@"server=localhost;database=数据库名;uid=sa;pwd=密码";
解决方案十二:
测试下连接字符串是否正确.服务器开没开.
解决方案十三:
<connectionStrings><addname="libraryConnectionString"connectionString="DataSource=(local);InitialCatalog=library;UserID=sa;Password=sa1234"providerName="System.Data.SqlClient"/></connectionStrings>这代码去掉看看
解决方案十四:
stringstrconn=ConfigurationSettings.AppSettings["dsn"];换成stringstrconn=Convert.ToString(ConfigurationManager.ConnectionStrings["libraryConnectionString"]);试试
解决方案十五:
<appSettings><addkey="dsn"value="server=127.0.0.1;userid=sa;password=sa1234;database=library"/></appSettings>把第一句改成这样看看
解决方案:
14楼的你那和LOCAL有什么区别!~~
解决方案:
好象是本机上用(local)而Web上用localhost吧新手帮顶了..
解决方案:
怀疑你的sqlserver服务没打开