当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net - Keyword not supported: 'server(local); database'

问题描述:

my web.config is

<connectionStrings>

<add name="taraznegarConnectionString" connectionString="Data Source=.;Initial Catalog=taraznegar;Integrated Security=True"

providerName="System.Data.SqlClient" />

</connectionStrings>

and this is my code:

namespace taraz

{

public class DAL

{

string connection = "Data Source=.;Initial Catalog=taraznegar;Integrated Security=True";

SqlConnection con;

SqlCommand cmd;

SqlDataAdapter da;

DataTable dt;

public DAL()

{

con = new SqlConnection();

con.ConnectionString = connection;

cmd = new SqlCommand();

cmd.Connection = con;

da = new SqlDataAdapter();

da.SelectCommand=cmd;

dt = new DataTable();

}

public DataTable ExecuteReader(string SQL)

{

connect();

da.SelectCommand.Connection = con;

da.SelectCommand.CommandText = SQL;

try

{

da.SelectCommand.ExecuteReader();

}

catch

{

da = null;

}

disconnect();

da.Fill(dt);

return dt;

}

public string ExcuteNonQuery(string SQL)

{

string result=null ;

cmd.CommandText = SQL;

connect();

try

{

cmd.ExecuteNonQuery();

}

catch { result = "خطا"; }

disconnect();

return result;

}

public string ExecuteScalare(string sql)

{

string result = null;

cmd.CommandText = sql;

connect();

try {

result = cmd.ExecuteScalar().ToString();

}

catch { result = "خطا"; }

disconnect();

return result;

}

void connect()

{

if (con.State == ConnectionState.Closed)

con.Open();

}

void disconnect()

{

if (con.State == ConnectionState.Open)

con.Close();

}

}

}

and when I'm using this class in my project the error is:

Server Error in '/' Application.

Keyword not supported: 'server(local); database'.

what's the probleam?

网友答案:

When you have a ConnectionString in your webconfig file then in your application you don't need to create another connection. You should use that connection. In your class DAL you are creating a new connection string. Instead of that you should use something like this-

string connectionString = ConfigurationManager.ConnectionStrings["taraznegarConnectionString"].ConnectionString; 
网友答案:

For Entity Framework (database-first or model-first; when you have a physical EDMX model file) you need to use a special type of connection string which is quite different from the straight ADO.NET connection strings everyone else has mentioned so far...

The connection string must look something like:

<connectionStrings>
    <add name="testEntities" 
         connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;Initial Catalog=taraznegar;Integrated Security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" 
         providerName="System.Data.EntityClient" />
</connectionStrings>

Inside this connection string, you'll find the provider connection string= attribute which is basically your ADO.NET connection string:

分享给朋友:
您可能感兴趣的文章:
随机阅读: