Enable to connect oracle database with asp.net windows service - asp.net

<connectionStrings>
<add connectionString="provider=MSDAORA;Data Source=MyOracleDB;User Id=user;Password=****;" name="con"/>
</connectionStrings>
This is my connection string. i am using visual studio 2013 and oracle 10g version.
This is my code.
OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
public DataTable GetTday()
{
conn.Open();
string query = "select * from tday";
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
conn.Close();
return dt;
}
it shows 'ORA-12154: TNS:could not resolve the connect identifier specified' this error. please someone help

Related

SqlException after moving connection string to web.config

I have to change my data source to the name of my server [server name]\SQLExpress after moving my connection string from Page_Load to web.config. Otherwise I get an SqlException at con.Open(). The app works fine both ways am just wondering why I need to change the datasource name after moving the connection string.
<connectionStrings>
<add name="CS"
connectionString="data source=[server name]\SQLEXPRESS; Initial Catalog = SampleDb; integrated security = true"
providerName="System.Data.SqlClient"/>
</connectionStrings>
(string cs = "data source=.\\SQLExpress; initial catalog = SampleDb; integrated security= true";)-moved to web.config
string CS = ConfigurationManager.ConnectionStrings["CS"].ConnectionString;
//pass the connection string to the SqlConnection() constructor
SqlConnection con = new SqlConnection(CS);
//tell sqlcommand what query to execute and which connection
SqlCommand cmd = new SqlCommand("select * from Electronics", con);
//open connection
con.Open();
//execute the command
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
//close the connection
con.Close();

Con.open error ASP.NET in Godaddy Plesk Hosting

I am struggling with my new website which I recently uploaded on Godaddy server.
Con.open() is the line which is causing error : The wait operation timed out
My Code: file.aspx.cs
SqlConnection con = new SqlConnection(#"Data Source=databaseserverurl;Network Library=DBMSSOCN;Initial Catalog=mydatabase;User ID=something;Password=something;");
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
if (con.State != ConnectionState.Open) {
con.Open();
}
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from mytable";
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
}
For MySQL
You should use MySqlConnection, not SqlConnection.
For SQL Server
Most likely your databaseserverurl is wrong. Make sure your connection string corresponds to the one you have in the control panel. See https://de.godaddy.com/help/find-your-connection-strings-3323.
To ensure, open Sql Server Management Tool (or your favorite sql tool) and login your db with same server, user and password.

Not able to access .DBF file in asp.net

I am not able read .dbf file in asp.net.
Error : External table is not in the expected format.
1) Using OLEDB
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\VisualFoxproDB;Extended Properties=dBASE 5.0;");
OleDbCommand command = new OleDbCommand("SELECT ID,NAME FROM table.dbf", conn);
conn.Open();
DataTable dt = new DataTable();
dt.Load(command.ExecuteReader());
gv1.DataSource = dt;
gv1.DataBind();
conn.Close();
.
2. Using ODBC
OdbcConnection conn = new OdbcConnection("Driver={Driver do Microsoft dBase (*.dbf)};DriverId=277;SourceType=DBF;SourceDB=D:\\VisualFoxproDB\\;Exclusive=No");
conn.Open();
string strQuery = "SELECT * FROM D:\\VisualFoxproDB\\test.dbf";
System.Data.Odbc.OdbcDataAdapter adapter = new System.Data.Odbc.OdbcDataAdapter(strQuery, conn);
System.Data.DataSet ds = new System.Data.DataSet();
adapter.Fill(ds);
return ds.Tables[0];

Fetching data from remote SQL server

I have developed a asp.net web application using visual studio 2010. I'm using SQL server 2008 as database. I need to fetch data from one of the remote server through LAN connection and have to store to my database through C# code. I am beginner to web development using asp.net and C#. please tell me a very simple way to solve this.
This is an example which I tried. Is this code OK or wrong please guide me with this?
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con1 = new SqlConnection();
con1.ConnectionString = #"Data Source=172.16.7.127\inhouse;User ID=****;Password= *****;";
con1.Open();
string str = "select * from t_his_events Where patientMRN="+hosp_no.Text;
SqlDataReader dr;
SqlCommand myCommand = new SqlCommand(str,con1);
dr = myCommand.ExecuteReader();
if (dr.HasRows)
{
Console.WriteLine("connection established");
}
}
using System.Data.SqlClient;
SqlConnection conn = new SqlConnection(<connectionstring>);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM table";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
And now you have a DataSet containing the results of your query. As for determining your , I suggest Connection Strings as a great resource for picking the perfect format.
Where is your database name in connectionString you created on remote server
using System.Data.SqlClient;
using system.Data;
string cs = Data Source=172.16.7.127\inhouse;database=databasename;User ID=User id create on remote server;Password= Your password for the ;
SqlConnection conn = new SqlConnection(cs);
SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM tableName",conn);
DataSet ds = new DataSet();
adp.Fill(ds);
now you can use the dataset object that contain your table

Calling a stored procedure with asp.net

If I have a connection string defined in my web.config file, how do I create a connection to the SQL db from C# code (sorry forgot to specify) and then call a stored procedure. I would then like to eventually use this data in some way as my DataSource for a GridView.
Here is how the connection string is defined in the web.config:
<connectionStrings>
<add name="db.Name" connectionString="Data Source=db;Initial Catalog=dbCat;User ID=userId;Password=userPass;" providerName="System.Data.SqlClient" />
</connectionStrings>
The db server is a Microsoft SQL server.
Here is what I was looking for:
ConnectionStringSettings conSet = ConfigurationManager.ConnectionStrings["db.Name"];
SqlConnection con = new SqlConnection(conSet.ConnectionString);
The code to get the data is fairly trivial. I was more interested in accessing it from a connectionString variable in the web.config file.
If it's a resource file like so:
private static readonly string connString = Resource1.connString;
Where connString is the name of the key. If it is a web.config file
Something like so:
private static readonly string connString = System.Configuration.ConfigurationManager.AppSettings["strConn"]; where conn is defined in your web config file.
<add key="strConn" value="User ID=test;Password=test;Initial Catalog=TestDB;Data Source=NameOfServer;"/>
Then call the sproc:
//connString = the string of our database app found in the resource file
using (SqlConnection con = new SqlConnection(connString))
{
using (SqlCommand cmd = new SqlCommand("EMPDLL_selClientByClientID", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#ClientID", SqlDbType.VarChar).Value = cID;
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
if (reader.Read())
{
//more code
}
}
}
}
}
That's if you are coding in C#, VB.net its the same deal just a bit more wordier :), here's a small sample:
Public Sub DeleteEmployee(ByVal lVID As Long)
Dim conMyData As SqlConnection
Dim cmdDelete As SqlCommand
Try
conMyData = New SqlConnection(connString)
cmdDelete = New SqlCommand("delEmployee", conMyData)
With cmdDelete
.CommandType = CommandType.StoredProcedure
'add the parameters
.Parameters.Add("#LoginID", SqlDbType.BigInt).Value = lVID 'the request
conMyData.Open() 'open a connection
.ExecuteNonQuery() 'execute it
End With
Catch ex As Exception
Throw ex
Finally
cmdDelete = Nothing
conMyData.Close()
conMyData = Nothing
End Try
End Sub
Of course you should use a using statement instead of try/catch/finally to ensure you clean up your resources that are being used.
Something like this...
using (var con = new SqlConnection(_connectionString))
{
using (var cmd = new SqlCommand(_storedProcedureName, con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#pMyParamater", myParamaterValue);
con.Open();
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
// do something with the row
}
}
}
}
This is all pretty simple stuff to be honest, you should be able to find everything you need from the ADO.NET documentation

Resources