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

本文关键字:连接 字符串 之间 Server net SQL asp | 更新日期: 2023-09-27 18:10:07

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

一般来说,这段代码的目的是使用两个文本框向数据库中添加两组数据。

这个错误在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();
        }
    }
}

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

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

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类成员

Solution2:

需要按如下方式导入System.Data.SqlClient; Namespace

using System.Data.SqlClient;

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

connectionString="data source=.'SQLEXPRESS;initial catalog=test;集成安全= SSPI";

EDIT:尝试以下代码获取exceptiondetails

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

有几个错误。首先,您在配置中的connectionString属性值之外放置了分号(;),它应该是:

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

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

using System.Data.SqlClient

我认为你应该有这样的配置

<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=,在我看来,这是更清晰和更明显的名称)

这个网站http://www.connectionstrings.com/是关于如何建立有效连接字符串的一个很好的和广泛的资源。

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

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

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

你也错过了命名空间,

using System.Data.SqlClient