使用Web配置文件时连接出错 [英] Error in connecting when using the web config file
问题描述
Web配置代码如下
< connectionstrings >
< add name = dbconnection connectionstring = Mymachine \ SQLSERVER2012; Integrated Security = true; Initial Catalog = Testing providername = System.Data.SqlClient / >
< / connectionstrings >
.aspx代码如下
protected void Page_Load(object sender,EventArgs e)
{
if(!IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string strConnString = ConfigurationManager.ConnectionStrings [dbconnection ] .ConnectionString;
using(SqlConnection con = new SqlConnection(strConnString))
{
using(SqlCommand cmd = new SqlCommand())
{
cmd.CommandText =select Tb_Employee ;
cmd.Connection = con;
con.Open();
gvEmpdetails.DataSource = cmd.ExecuteReader();
gvEmpdetails.DataBind();
con.Close();
}
}
}
当我运行应用程序时显示如下错误
>不支持关键字:'Mymachine \sqlserver2012;综合保障'
请在上面的代码中出现什么错误
我有什么试过:
网页配置代码如下
< connectionstrings >
< add 名称 = dbconnection connectionstring = Mymachine \ SQLSERVER2012; Integrated Security = true; Initial Catalog = Testing providername = System.Data.SqlClient / >
< / connectionstrings >
protected void Page_Load(object sender,EventArgs e)
{
if(! IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string strConnString = ConfigurationManager.ConnectionStrings [dbconnection ] .ConnectionString;
using(SqlConnection con = new SqlConnection(strConnString))
{
using(SqlCommand cmd = new SqlCommand())
{
cmd.CommandText =select Tb_Employee ;
cmd.Connection = con;
con.Open();
gvEmpdetails.DataSource = cmd.ExecuteReader();
gvEmpdetails.DataBind();
con.Close();
}
}
}
.aspx代码如下
< pre lang = HTML >
当我运行应用程序时显示错误如下
>不支持关键字:'Mymachine \sqlserver2012;综合安全'
请在上面的代码中出现什么错误
试试以下内容 -
< connectionstrings >
< add 名称 = dbconnection connectionstring = server = Mymachine \ SQLSERVER2012; Integrated Security = true; Initial Catalog = Testing providername = System.Data.SqlClient / >
< / connectionstrings >
或,
< connectionstrings >
< 添加 名称 = dbconnection connectionstring = 数据源= Mymachine \ SQLSERVER2012;综合安全=真;初始目录=测试 providername = System.Data.SqlClient / >
< / connectionstrings >
你缺少server =提供服务器名称时,或数据源=在连接字符串中。
希望,它有帮助:)
检查你的连接 string 在 Web.config文件中它应该是 $>格式
< add name =ConnectionStringName>
providerName =System.Data.SqlClient
connectionString =Data Source = ServerName; Initial Catalog = DatabaseName; Integrated Security = False; User Id = userid; Password = password;>
其连接字符串错误
使用以下语法
Server = myServerName \ myInstanceName; Database = myDataBase; User Id = myUsername;
密码= myPassword;
从您的代码中看起来您错过了'服务器'和'数据库'keyword。
纠正它们。它将解决您的问题
Web config code as follows
<connectionstrings>
<add name="dbconnection" connectionstring="Mymachine\SQLSERVER2012; Integrated Security=true; Initial Catalog=Testing" providername="System.Data.SqlClient" />
</connectionstrings>
in .aspx code as follows
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string strConnString = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText ="select Tb_Employee";
cmd.Connection = con;
con.Open();
gvEmpdetails.DataSource = cmd.ExecuteReader();
gvEmpdetails.DataBind();
con.Close();
}
}
}
When i run the application shows error as follows
>Keyword not supported: 'Mymachine\sqlserver2012; integrated security'
please what is the mistake in my above code
What I have tried:
Web config code as follows
<connectionstrings>
<add name="dbconnection" connectionstring="Mymachine\SQLSERVER2012; Integrated Security=true; Initial Catalog=Testing" providername="System.Data.SqlClient" />
</connectionstrings>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string strConnString = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText ="select Tb_Employee";
cmd.Connection = con;
con.Open();
gvEmpdetails.DataSource = cmd.ExecuteReader();
gvEmpdetails.DataBind();
con.Close();
}
}
}
in .aspx code as follows
<pre lang="HTML">
When i run the application shows error as follows
>Keyword not supported: 'Mymachine\sqlserver2012; integrated security'
please what is the mistake in my above code
Try following-
<connectionstrings> <add name="dbconnection" connectionstring="server=Mymachine\SQLSERVER2012; Integrated Security=true; Initial Catalog=Testing" providername="System.Data.SqlClient" /> </connectionstrings>
or,
<connectionstrings> <add name="dbconnection" connectionstring="Data Source=Mymachine\SQLSERVER2012; Integrated Security=true; Initial Catalog=Testing" providername="System.Data.SqlClient" /> </connectionstrings>
You are missing "server=" or "Data Source=" in your connection string while providing the server name.
Hope, it helps :)
check your connection string in Web.config file it should be in below format
<add name="ConnectionStringName">
providerName="System.Data.SqlClient"
connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=False;User Id=userid;Password=password;>
Its connection string error
use below syntax
Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername; Password=myPassword;
and from your code it looks you have missed 'Server' and 'Database' keyword.
correct them. it will resolve your issue
这篇关于使用Web配置文件时连接出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!