Connecting to SQL Server database with Mono C# - c#

I am trying to migrate my service cross-platform with mono, but in attempting to connect to a SQL Server database I get the following timeout error
Timeout expired. The timeout period elapsed prior to the completion of the operation or the server is not responding. at Mono.Data.Tds.Protocol.TdsComm..ctor
at System.Data.SqlClient.SqlConnection.Open()
Databases are fairly new to me, but as far as I can tell from here (Google cache page, mono site is down) accessing SQL Server databases is now possible in Mono. Is that correct?
I attempted to structure my connection string as shown, but still no luck. My simple test code...
using (SqlConnection con = new SqlConnection(cs))
{
con.Open();
SqlDataReader reader = null;
SqlCommand cmd = new SqlCommand("SELECT Parameter FROM Deltas", con);
reader = cmd.ExecuteReader();
reader.Read();
Console.WriteLine(reader["Parameter"].ToString());
con.Close();
}
Am I missing any references or is my format incorrect? How can I connect using Mono C#?
EDIT:
Connection String, Defined globally and init in constuctor:
cs = #"Server=xxx.xxx.xxx.xxx;
Database=myDB;
User ID=user;
Password=passwd;";
cs = #"Data Source=xxx.xxx.xxx.xxx;
Network Library=DBMSSOCN;
Initial Catalog=myDB;
User ID=user;
Password=passwd;";
Top is me trying to conform to the mono example, bottom is what works with the .NET runtime.

It seems you hit a bug:
http://www.mail-archive.com/mono-bugs#lists.ximian.com/msg50686.html
quote:
This only happens when using the .NET 2.0 version of TdsComm - when
compiling with .NET 1.0 (mcs instead of gmcs), the connection also
works.

Related

Not able to connect access database to visual studio

I am trying to conncet access database to my visual studio but i face this type of error "The Microsoft.ACE.OLEDB.12.0" provider is not registered on the local machine.
I look at some solutions on google i got that after downloading Microsoft Access Database Engine on Local machine we are able to connect database.But i still face this problem. So What should i do for it? .And i am using Windows10 but i use Ms office 2007 and my system is 64-bit.enter image description here
You don't mention if you planning to use the x32 bit or x64 version of access.
So, assuming you downloaded the access data engine? You have to choose or decide WHICH bit size version you wish to use.
That download can be found here:
https://www.microsoft.com/en-ca/download/details.aspx?id=13255
And you have two choices:
Ok, next up?
You MUST set and FORCE your project to the correct bit size.
that is this project setting in VS.
So, if you using x32 version, then use x86 version, and if adopting x64 version, then set your project to x64.
So, don't use "any" for your project setting - you have to force the issue.
So, try setting up a connection. You have two choices:
Use the ODBC provider,
or
Use the oleDB provider
So, ok, ODBC? then that connection setting should look like this:
For odbc, the connecton string should look like this:
Driver={Microsoft Access Driver (*.mdb, *.accdb)};
dbq=C:\TEST\test444.accdb;uid=Admin
for oleDB, the connection string should look like this:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\test\test45.accdb
And in both cases, you can as a normal rule, leave the password blank.
Assuming you used the project settings, say here:
(and you can use the connection string builder in above. BUT BE VERY carefull, if you are running access x64, then the test connection WILL ALWAYS ALWAYS fail, since VS is a x32 bit applcation, but if you run your code, and the project was forced to x64 bits, then the connection will work DISPITE VS and the test connection telling you it does not.
So, to use the above ODBC connection, we have this:
using (OdbcConnection conn = new OdbcConnection(Properties.Settings.Default.AccessODBC))
{
string strSQL =
"SELECT * FROM tblHotels ORDER BY HotelName";
using (OdbcCommand cmdSQL = new OdbcCommand(strSQL, conn))
{
conn.Open();
DataTable rstData = new DataTable();
rstData.Load(cmdSQL.ExecuteReader());
dataGridView1.DataSource = rstData;
}
}
And for oledb, we would have this:
using (OleDbConnection conn = new OleDbConnection(Properties.Settings.Default.AccessDB))
{
string strSQL =
"SELECT * FROM tblHotels ORDER BY HotelName";
using (OleDbCommand cmdSQL = new OleDbCommand(strSQL, conn))
{
conn.Open();
DataTable rstData = new DataTable();
rstData.Load(cmdSQL.ExecuteReader());
dataGridView1.DataSource = rstData;
}
}
So, which provider to use?
Most use and suggest oleDB.
However, I think using ODBC is not all that bad of a choice. the reason is you can change the connection to sql server, or other database systems, and NOT have to change your provider, but only the connection string.
While you can sort of do the same with oleDB, there is now MUCH better support for ODBC database systems.

Azure function Connecting to Oracle database

I have a console application to connect and update a record on oracle. This works fine. But after copying the same function onto an azure function, I am getting Connection Timeout issue. Does anybody know if there are some set ups or other configuration required on Azure?
Below is my code block:
OracleConnection con = new OracleConnection();
OracleConnectionStringBuilder ocsb = new OracleConnectionStringBuilder();
ocsb.Password = "xxxxx";
ocsb.UserID = "xxxxx";
ocsb.DataSource = "xxxxxxx";
con.ConnectionString = ocsb.ConnectionString;
con.Open();
OracleCommand cmd = con.CreateCommand();
cmd.CommandText = "UPDATE PRODUCTS SET STATUS = 20 WHERE STATUS = '30'";
cmd.CommandTimeout = 100000;
cmd.ExecuteNonQuery();
con.Close();
Partially answer for connecting to on-premise service like Oracle-db from Azure Functions, there is an existing SO thread had answered it, which you can refer to.
So first, you must make sure networking access to on-premise server available. Following this article about using Hybrid Connections.
Then, if you want to query oracle database via odbc, the oracle odbc driver must be installed on the client-side.
THanks for your help above,
The issue occurred because I made an update on the oracle console but did not commit. Unlike sql server, when you run an update statement, you should also run the 'commit' line of code.

Getting 12154: TNS:could not resolve the connect identifier specified error with .NET

I've written this simple connection in my code:
OracleConnection con = new OracleConnection("Data Source=ORCL;User
ID=agapus;Password=agap");
con.Open();
Then I run my application pressing F5 and I get the above error. This error occurs no matter if it's just a simple console app or an ASP.NET web site. I've tried both Oracle.DataAccess and the old windows oracle API. I've tried several Oracle services, all with no luck.
The environment where I'm testing this connection is a Windows Server 2008R2. I can easily connect to any database using SQLPlus or TOAD. I've tried explicitly specifying the location of the TNS_NAMES.ORA file, doesn't work either.
The only thing that works is when I specify the full path in the connection string. So if I change the above code into the below one, it'll work:
string conString = "user id=agapus;password=agap;data source=(DESCRIPTION=(ADDRESS=
(PROTOCOL=tcp)(HOST=172.16.0.121)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL)))";
ID=agapus;Password=agap");
OracleConnection con = new OracleConnection(conString);
con.Open();
If you look in your sqlnet.ora file, I believe that you will find that the default domain is set to .world .
That would make the correct name of your connection ORCL.world .
You may also have .world on the end of your connection name in TNSNAMES.ora, and nothing for the default domain.
What do you get when you run tnsping?

Best way to Connect to SQL server 2000 with ASP 4.0 on .NET 4.0 build

Can I do this using SqlClient or do I have to use the ODBC route > ? I get error's in VS but from reading through the other post it appears to be an IDE specific problem.
The best way is to use ADO.NET which should work just fine. You should absolutely stay away from the ODBC route:
using (var conn = new SqlConnection("Data Source=mySql200Server;Initial Catalog=myDataBase;User Id=user;Password=secret;"))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "SELECT count(*) FROM foo";
var result = cmd.ExecuteScalar();
}
If this doesn't work here are the things to check:
Your connection string is correct (serverName/database/username/password)
You have access to the SQL server from within your ASP.NET server (verify network and firewall)
Your SQL query is correct
Use Microsoft Enterprise Library 5.0, specifically the Data Access application block.
http://msdn.microsoft.com/en-us/library/ff632023.aspx
http://www.codeproject.com/KB/architecture/MS-EntLib-DataAccess1.aspx
http://jwalin.wordpress.com/2009/02/11/get-started-with-the-enterprise-library-data-access-application-block/
Yes, you can use SqlClient, and you shouldn't get any errors.

Querying a remote Indexing Service database

I'm trying to search the Indexing Service of a remote Windows 2003 server from ASP.NET. There's sample code for this on the MS site, but only for local searches. Here's what I've got so far; the remote server in this example is called "indexserver", isn't on a domain, and has a index called "system":
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Provider=MSIDXS;User ID=administrator;Password=Password";
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText= "SELECT * FROM indexserver.system..FILEINFO";
conn.Open();
cmd.ExecuteReader();
Running this gives me the error "Multiple-step OLE DB operation generated errors. Check the OLE DB status if available. No work was done".
Does anyone know how to get this working? All I need to do is query the Index for a filename and get the path of that file back.
Never mind, I discovered that Windows Indexing Service is somewhat depreciated,and Windows Search seems to be the way to go...

Categories