连接asp.net, c#和SQL server(两个版本都是2012)
本文关键字:两个 asp 版本 2012 SQL 连接 server net | 更新日期: 2023-09-27 18:10:08
我在asp.net中编写了一个连接字符串,即通过2个文本框添加2个数据到SQL server数据库。但在按下提交按钮后,我面对一个错误。
细节:
web . config:
<!--
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;
using System.Collections.Generic;
using System.Linq;
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_Click1(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 Table_1 (name,fathername) VALUES('" + txt1.Text + "','" + txt2.Text + "')", con);
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
String ErrorMsg = ex.ToString();
}
finally
{
con.Close();
}
}
}
}
错误提示:
Server Error in '/' Application.
Keyword not supported: 'initial catalogue'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: Keyword not supported: 'initial catalogue'.
Source Error:
Line 21: {
Line 22: string cs = System.Configuration.ConfigurationManager.ConnectionStrings["myconectionstring"].ConnectionString;
Line 23: SqlConnection con = new SqlConnection(cs);
Line 24:
Line 25: try
Source File: c:'Users'Admin'Documents'Visual Studio 2012'Projects'WebApplication3'WebApplication3'WebForm1.aspx.cs Line: 23
您的连接字符串应该如下开始:
Server=myServerAddress;Database=myDataBase;
而不是当前
data source=.'SQLEXPRESS;initial catalogue=test;
使用说明:
Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;
当您忘记任何数据库的连接字符串时,请转到:
http://www.connectionstrings.com你把Catalog拼错了。它的Initial Catalog
这是Initial Catalog
的一种类型,纠正拼写,它应该工作!!
查看更多信息:-
http://www.connectionstrings.com/sql-server-2012/