C# ErrorSystem.Data.OleDb.OLEDBEException(0x80004005):ORA-12504:TNS:未在CONNECT_数据中为侦听器提供服务_名称
我正在从事一个c#项目,在visual studio中注册一个新用户,并使用toad作为数据库。连接到数据库的代码是:C# ErrorSystem.Data.OleDb.OLEDBEException(0x80004005):ORA-12504:TNS:未在CONNECT_数据中为侦听器提供服务_名称,c#,asp.net,C#,Asp.net,我正在从事一个c#项目,在visual studio中注册一个新用户,并使用toad作为数据库。连接到数据库的代码是: protected void New_User(object sender, EventArgs e) { OleDbConnection conn; OleDbDataAdapter OledbAdapter = new OleDbDataAdapter(); String sql = ""; co
protected void New_User(object sender, EventArgs e)
{
OleDbConnection conn;
OleDbDataAdapter OledbAdapter = new OleDbDataAdapter();
String sql = "";
conn = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
sql = "INSERT INTO USER_DETAIL VALUES('" + TextBoxUN.Text + "','" +TextBoxPass.Text+ "', '" + TextBoxEmail.Text + "','" + TextBoxmobileno.Text +"')";
try
{
conn.Open();
OledbAdapter.InsertCommand = new OleDbCommand(sql, conn);
OledbAdapter.InsertCommand.ExecuteNonQuery();
Label_success.Text = "Successfully inserted into database";
}
catch (Exception ex)
{
Label_success.Text = "Error" + ex.ToString();
}
finally
{
conn.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="ConnString" connectionString="Provider=MSDAORA;Data Source=TRGDB;Persist Security Info=True;Password=pass;User ID=TRAINEE01" providerName="System.Data.OleDb"/>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
</providers>
</profile>
<roleManager enabled="false">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
</providers>
</roleManager>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
但我得到了以下错误:
ErrorSystem.Data.OleDb.OLEDBEException(0x80004005):ORA-12504:TNS:侦听器在System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr,OLEDBConnectionConnection connection)的System.Data.OleDb.OleDbConnectionFactory.CreateConnection中的CONNECTOR(OleDbConnectionString construct,OLEDBConnectionConnection connection)中未被赋予服务名称(数据库连接选项、对象池组ProviderInfo、数据库连接池池、数据库连接所有者对象)位于System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(数据库连接所有者连接、数据库连接池组池组)位于System.Data.ProviderBase.DbConnectionFactory.GetConnection(数据库连接所有者连接)项目中System.Data.OleDb.OleDbConnection.Open()的System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection,DbConnectionFactory connectionFactory)的默认值。新用户(对象发送方,事件参数e)在c:\documents and settings\admin\my documents\visual studio 2010\Project\Project\Default.aspx.cs中:第26行
您的连接字符串应如下所示: 数据源=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort))(CONNECT_Data=(SERVICE_NAME=MyOracleSID)); 用户Id=我的用户名;密码=我的密码
请参阅:不要使用字符串连接向SQL语句中添加值:改为ParameterSie。一旦连接到数据库,您将容易受到SQL注入的攻击。