Asp.Net应用程序数据库连接问题

时间:2012-07-24 21:07:15

标签: c# asp.net sql-server-2008 ado.net

当我尝试使用C#连接我的应用程序到我的数据库时,我在Visual Studio 2005中成功构建了应用程序但是当我在调试器中运行该站点时出现错误:

Exception Details: System.ArgumentException: Format of the initialization string does not conform to specification starting at index 118.

以下是给出错误的连接代码:

SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Server"].ConnectionString);

我在web.config文件中正确编写了连接字符串,因此我对此远程意味着什么无能为力。我不确定我是否遗漏了任何东西。任何帮助赞赏。以下是我可能有用的部分的完整代码:

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();
    }
}
}

如果我遗漏了一些有用的数据,请告诉我。

继承连接字符串:

       

3 个答案:

答案 0 :(得分:7)

我已经分离出来并且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

如您所见,您在;Application Name之间错过了User ID。我不确定这是否是 问题,但这是可能的。

答案 1 :(得分:2)

您的连接字符串使用Integrated Security=True,但语法正确 Integrated Security=SSPI;Trusted_Connection=True如此更改并删除UserId和密码 (或删除Integrated Security = True并保留UserID和密码)

尝试更改代码中的内容。

protected void Search_Click(object sender, EventArgs e) 
{ 
    string IDnumber = mechantNumber.Text; 
    string selectSQL = "SELECT * FROM Authors Where MID=@num"; 
    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状态修正,这可以保证处理您的连接 尝试使用参数化查询,这样可以避免Sql Injection攻击和引用问题 SELECT也需要字段列表或*

答案 2 :(得分:2)

我认为雅各布是对的。另外,请不要这样做:

string selectSQL = "SELECT FROM Authors Where MID='"+ IDnumber +"'";

这将导致sql注入攻击,就像上周用于获取雅虎用户帐户的攻击一样。

请改为:

 string selectSQL = "SELECT * FROM Authors Where MID=@ID"; 
 cmd.Parameters.AddWithValue("@ID", IDnumber );