Asp.Net应用程序数据库连接问题
当我尝试在C#中编写到我的数据库的应用程序连接时,我在Visual Studio 2005中成功构建应用程序,但是当我在调试器中运行该网站时,出现错误:Asp.Net应用程序数据库连接问题
Exception Details: System.ArgumentException: Format of the initialization string does not conform to specification starting at index 118.
下面是给错误的连接代码:我有我的web.config正确写入连接字符串
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Server"].ConnectionString);
,什么这意味着远程文件,以便即时无言以对。林不知道如果我失去了什么。任何帮助赞赏。这里是我可能帮助的部分的全部代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using System.Data.SqlClient;
public partial class RateAnalyzerPage: System.Web.UI.Page
{
protected void Search_Click(object sender, EventArgs e)
{
string IDnumber = mechantNumber.Text;
string selectSQL = "SELECT FROM Authors Where MID='"+ IDnumber +"'";
// SqlConnection con = new SqlConnection(@"Server=");
//SqlConnection con = new SqlConnection();
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Server"].ConnectionString);
SqlDataReader reader;
try
{
con.Open();
SqlCommand cmd = new SqlCommand(selectSQL, con);
reader = cmd.ExecuteReader();
while (reader.Read())
{
QualVol.Text = reader["TotalVolume"].ToString();
}
reader.Close();
}
catch (Exception err)
{
}
finally
{
con.Close();
}
}
}
让我知道是否缺少一些有用的数据。
继承人的连接字符串:
我分离出来并用XML解码连接字符串值:
Data Source=Server;Initial Catalog=Odata;Integrated Security=True;MultipleActiveResultSets=False;Packet Size=4096;Application Name="Microsoft SQL Server Management Studio"User ID=Name;Password=PW
As你可以看到,你错过了Application Name
和User ID
之间的;
。我不确定这是问题,但这是可能的。
连接字符串使用Integrated Security=True
,但正确的语法是 Integrated Security=SSPI;
或Trusted_Connection=True
所以改变它,要删除用户名和密码。
(或删除集成安全性= True并保留用户ID和密码)
尝试更改代码中的内容。
protected void Search_Click(object sender, EventArgs e)
{
string IDnumber = mechantNumber.Text;
string selectSQL = "SELECT * FROM Authors Where [email protected]";
using(SqlConnection con = new SqlConnection
(System.Configuration.ConfigurationManager.ConnectionStrings
["Server"].ConnectionString))
{
SqlDataReader reader;
con.Open();
SqlCommand cmd = new SqlCommand(selectSQL, con);
cmd.Parameters.AddWithValue("@num", IDNumber);
reader = cmd.ExecuteReader();
while (reader.Read())
{
QualVol.Text = reader["TotalVolume"].ToString();
}
reader.Close();
}
}
尝试使用using
statemend,这保证了您的连接所述放置
尽量使用参数化查询,这避免SQL注入攻击和报价问题
还可以选择需要的字段列表或*
是一个快速更改:PI表示服务器。 – 2012-07-24 21:23:12
使用IntegratedSecurity = True和UserID/Password更新了答案。 – Steve 2012-07-24 21:30:30
我认为雅各布是对的。请注意,不要这样做:
string selectSQL = "SELECT FROM Authors Where MID='"+ IDnumber +"'";
这将导致SQL注入攻击,就像上周用来获取雅虎用户帐户的攻击一样。
而是执行此操作:
string selectSQL = "SELECT * FROM Authors Where [email protected]";
cmd.Parameters.AddWithValue("@ID", IDnumber);
什么行给出了错误,当您打开连接?你可以发布你的web.config connectionStrings部分? – 2012-07-24 21:09:27
你需要发布你的''connectionStrings'部分。配置文件;这是最有可能发生错误的地方 – GolfWolf 2012-07-24 21:09:45
'code' \t \t connectionStrings>'code' –
2012-07-24 21:20:28