asp.net和SQL Server之间的连接字符串

时间:2014-06-25 06:41:45

标签: c# asp.net sql-server

我写了一些代码来获取asp.net和SQL Server之间的连接,但我有

通常,此代码的目的是使用2个文本框将2组数据添加到数据库中。

此错误发生在web.config文件中:

  

错误1 Character ';', hexadecimal value 0x3b is illegal in an XML name.

我的配置:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="myconectionstring"
    connectionString="data source=.\SQLEXPRESS;initial catalogue=test;Integrated Security=SSPI"; providerName="System.Data.SqlClient" />
  </connectionStrings>
    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>
</configuration>

在代码背后:

  

找不到类型或命名空间名称'SqlConnection'(您是否缺少using指令或程序集引用?)

我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{

    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string cs = System.Configuration.ConfigurationManager.ConnectionStrings["myconectionstring"].ConnectionString;
            SqlConnection con = new SqlConnection(cs);
            SqlCommand cmd = new SqlCommand("INSERTINTO TEST (name,fathername) VALUES('" + TextBox1.Text + "','" + TextBox1.Text + "')", con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}

最好的问候:)

=========================================

编辑:我已根据您的建议完成,并且没有更多错误但数据未添加到数据库中,实际上当我点击提交时没有任何反应。 :(

我的代码:

的web.config:

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>

  <connectionStrings>
    <add name="myconectionstring"
    connectionString="data source=.\SQLEXPRESS;initial catalogue=test;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
  </connectionStrings>

    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>

</configuration>
代码背后的代码:

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace WebApplication3
{

    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            string cs = System.Configuration.ConfigurationManager.ConnectionStrings["myconectionstring"].ConnectionString;
            SqlConnection con = new SqlConnection(cs);
            SqlCommand cmd = new SqlCommand("INSERT INTO Table_1 (name,fathername) VALUES('" + txt1.Text + "','" + txt2.Text + "')", con);
            con.Open();
            cmd.ExecuteNonQuery();

            txt1.Text = "";
            txt2.Text = "";

            con.Close();
        }
    }
}

4 个答案:

答案 0 :(得分:2)

错误1:您已在连接字符串的末尾添加了分号,如下所示:

connectionString="data source=.\SQLEXPRESS;initial catalogue=test;
       Integrated Security=SSPI";
                            ^^^^^

解决方案1:您需要在connectionstring

内移动分号

试试这个:

connectionString="data source=.\SQLEXPRESS;initial catalogue=test;
                                         Integrated Security=SSPI;"

错误2:您没有导入System.Data.SqlClient命名空间以使用SqlConnection班级成员

<强>溶液2:

您需要导入System.Data.SqlClient; Namespace asbelow

using System.Data.SqlClient;

解决方案3:初始目录拼写错误,您应该在连接字符串中使用Initial Catalog,如下所示:

connectionString =“data source =。\ SQLEXPRESS; initial catalog = test;            综合安全= SSPI“;

编辑:请尝试使用以下代码获取异常详细信息

protected void Button1_Click(object sender, EventArgs e)
    {
        string cs = System.Configuration.ConfigurationManager.ConnectionStrings["myconectionstring"].ConnectionString;
        SqlConnection con = new SqlConnection(cs);

       try
       {
        string cs = System.Configuration.ConfigurationManager.ConnectionStrings["myconectionstring"].ConnectionString;

        SqlCommand cmd = new SqlCommand("INSERT INTO TEST (name,fathername) VALUES('" + TextBox1.Text + "','" + TextBox1.Text + "')", con);
        con.Open();
        cmd.ExecuteNonQuery();           
       }
    catch(Exception ex)
    {
    String ErrorMsg=ex.ToString();
    }
   finally
   {
       con.Close();
   }
    }

答案 1 :(得分:0)

你有几个错误。第一个是你在分配应该是的connectionString属性值之外放置了分号(;):

<connectionStrings>
    <add name="myconectionstring"
    connectionString="data source=.\SQLEXPRESS;initial catalogue=test;Integrated Security=SSPI;" 
    providerName="System.Data.SqlClient" />
  </connectionStrings>

在您的代码文件中,您没有导入所需的命名空间,请尝试添加:

using System.Data.SqlClient

答案 2 :(得分:0)

我认为你应该有这个配置

<connectionStrings>
   <add name="myconectionstring"
        connectionString="data source=.\SQLEXPRESS;initial catalog=test;Integrated Security=SSPI;"
        providerName="System.Data.SqlClient" />
</connectionStrings>

<connectionStrings>
   <add name="myconectionstring"
        connectionString="server=.\SQLEXPRESS;database=test;Integrated Security=SSPI;" 
        providerName="System.Data.SqlClient" />
</connectionStrings>

使用Initial Catalogdatabase代替initial catalogue=(或使用server=database=,我认为这些名称更清晰,更明显/ p>

这个网站http://www.connectionstrings.com/是一个非常广泛的资源,关于如何构建有效的连接字符串。

当然,正如其他人提到的那样 - 您需要在代码文件中添加using System.Data.SqlClient

答案 3 :(得分:0)

错误说清楚了!从配置标记中的连接字符串中删除分号(;)。 如下图所示

  <connectionStrings>
    <add name="myconectionstring"
    connectionString="data source=.\SQLEXPRESS;initial catalogue=test;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
  </connectionStrings>

你也错过了命名空间,

using System.Data.SqlClient