Using SQLOLEDB in c# or wpf Devexpress - c#

just run into https://social.msdn.microsoft.com/Forums/sqlserver/en-US/0ff7f210-7697-431b-9e6b-0f2e10f4c031/sql-2016-slow-connection-using-server-name-fast-using-127001?forum=sqlsetupandupgrade.
Is it possible to simply bypasse .net in a c# program and simply have this kind of performance ?
Is this is juste a change in the connection in order to use this provider, or its something more complexe ?
Success: Provider=SQLOLEDB;Data Source=::1;Integrated Security=SSPI
Elapsed: 00:00:00.0015378
Here is how we do it now:
SqlCommand command =
new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
// Call Read before accessing data.
while (reader.Read())
{
...
}
// Call Close when done reading.
reader.Close();
Here is the connection string:
Server=...;Database=...;Uid=...;Pwd=...;Encrypt=True;TrustServerCertificate = true;Asynchronous Processing=true;MultipleActiveResultSets=True

That thread is from a user having slow connection times to local SQL Server using .NET's SqlClient, but I couldn't reproduce it.
Are you having some sort of actual problem?
And no, you can't use SqlClient with an OleDB provider or ODBC driver. There are different ADO.NET providers for those. And SQLOLEDB is deprecated, so SqlClient is the only modern, supported SQL Server connection stack that ships as part of Windows.

Related

SQLite connection string: 'Version' keyword is not supported in C#

I have a c# winform project that supposes to save data to SQLite database, I've already used the dll properly and it runs without error, but I get an exception when trigger the method with buttonClick event
here the exception i got : Keyword not supported :'version'.
this the connection string:
"Data Source = Diary.db;Version = 3;New = False;Compress = True;";
and this the complete method :
private void AddToDbaseSQL3()
{
try{
string query = "insert into Diary(title,date,mood,wheater,content)
values('"+TitleTextbox.Text+"','"
+dateTimePicker.Value.Date.ToString("yyyy-MM-dd HH:mm")+"','"
+MoodCombobox.SelectedItem+"','"
+WheaterCombobox.SelectedItem+"','"
+ContentTextbox.Text+"');";;
SqlConnection connect2 = new SqlConnection(connection2);
SqlCommand cmd = new SqlCommand(query,connect2);
SqlDataReader read;
connect2.Open();
read = cmd.ExecuteReader();
while(read.Read())
{
}
MessageBox.Show("created");
TitleTextbox.Text = "Title";
TitleTextbox.ForeColor = SystemColors.ControlLight;
ContentTextbox.Clear();
connect2.Close();
}catch(Exception e){
MessageBox.Show(e.Message);
}
}
I've looked to this link:
Keyword not supported: 'version'
and it said to change SqlConnection to SQLiteConnection but it ended with an error, can you tell what's is the right connection string ? or there is something wrong from my code/method? please tell me, thank you, I'm sorry because it's my first time using the SQLite
SqlConnection from System.Data.SqlClient is for SQL Server.
You need an dedicated SQLite ADO.NET provider.
You can found the System.Data.SQLite provider from SQLite team here:
https://system.data.sqlite.org
Or you can use any libre or commercial provider.
You can also use the free and open-source SQLite ODBC driver that works fine and allow to use VS Visual Designers to create strongly typed ADO.NET DataSets, in addition to the use of OdbcConnection, OdbcCommand and so on:
http://www.ch-werner.de/sqliteodbc
C# Reading data from existing SQLite database

Is there a way to send OdbcCommand.ExecuteReader-NonQuery commands to execute on an API?

I'm talking about a WinForm application that establishes a connection to a database via Odbc. But sometimes my VPN turns off but I have a server that is on the same network as the database.
Is there a way to override ExecuteReader function so that every OdbcCommand that wants to query is sent to a WebAPI and return the same object to the reader?
For example:
var cmd = new OdbcCommand("SELECT number, text FROM testing", Con);
var reader = cmd.ExecuteReader();
Then the reader obtains its value calling to https://example.com/api/tests/ and the code keeps working without VPN.
Please forget about security.

Unspecified Error when trying to ExecuteReader on SQL Server CE

I had implemented a database storage on my app for Windows Mobile 6.5 using SQL Server CE.
Had managed to install the SQL Server CE CAB file on the device (Motorola MC65).
Managed to create database file, and create tables. Insert also can be executed.
However when I try to run ExecuteReader() to read records, I hit the following error:
Error Code: 80004005
Message : Unspecified error
Minor Err.: 25534
Source : SQL Server Compact ADO.NET Data Provider
No idea why this is happening. Since insert can be executed I thought this should not be a connection or privilege issue.
The reading code is as below:
openConnection();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT NAME FROM GROUP_INFO ORDER BY NAME ";
SqlCeDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
string groupName = reader.GetString(0);
listGroup.Add(groupName);
}
The exception is thrown on the line where cmd.ExecuteReader() is executing.
Any pointers are appreciated. Thanks.
Implement proper error handling for SqlCeExceptions! The error is documented here https://technet.microsoft.com/en-us/library/ms172350(v=sql.110).aspx
Large objects (ntext and image) cannot be used in ORDER BY clauses.
Maybe you should redefine the column as nvarchar(4000) (it is currently ntext), or rephrase the query using:
ORDER BY CAST(Name as nvarchar(4000))
But this will cause a table scan

3 types of asking SQL (without mappers type linq or nhibernate)

what is difference between these 3 types of asking SQL for data (not telling that odbc can ask different DB)
1.) ODBC dll: Microsoft.Data.ODBC
OdbcConnection cn;
OdbcCommand cmd;
string MyString;
MyString="Select ...";
cn= new OdbcConnection("Driver={SQL Server};Server=...;UID=...;PWD=...;Database=...;");
cn.Open();
cmd=new OdbcCommand(MyString,cn);
OdbcDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
....
}
reader.Close();
cn.Close();
second dll: System.Data.SqlClient
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["…"].ConnectionString);
SqlDataReader rdr = null;
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT ..."
cmd.CommandType = CommandType.Text;
conn.Open();
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
.....
}
rdr.Close();
conn.Close();
third: dll: Microsoft.Practices.EnterpriseLibrary.Data
Database context = DatabaseFactory.CreateDatabase("...");
IDataReader dataReader = context.ExecuteReader(CommandType.Text,"SELECT ...")
dataReader.Read();
var inputid = (int)dataReader["..."];
ODBC is the very old, long gone DB-access standard - don't use it unless you absolutely must. (it was "hip" back in the 1990's or so)
SqlClient is the .NET bare-bones ADO.NET level of accessing SQL Server databases, and the foundation for all other technologies. It can basically do anything with SQL Server that you need to do - run data queries, run DDL queries, execute stored procedures and so forth. It's fairly basic and bare-bones, e.g. you have to type a lot of repeating, boring code yourself
EnterpriseLibrary.Data is a layer on top of ADO.NET which simplifies common tasks by handling some of the repetitive code for you.
There are additional things on top of the ADO.NET foundation - things like NHibernate, Entity Framework and so forth - but the SqlClient / ADO.NET is really the basis for all current, modern database access (to SQL Server, mostly) in the .NET world
ODBC drivers are a sort of wrapper around ODBC, that is a wrapper over native db connection
SqlConnection are the most direct way of talking to MSSQL server AFAIK
DatabaseFactory is just a factory ( ehm ... ) that eventually uses SqlConnection when you talk with MSSQL Server.
Microsoft.Practices.EnterpriseLibrary.Data is a wrapper around System.Data.SqlClient, so it adds some niceties to the built-in SqlClient functionality. As #Felice Pollano said, ODBC drivers wrap native DB connection, so I tend to avoid them.

How can I set a connection lifespan timeout for an OracleDataAdapter executing a stored procedure using the Fill Method?

I have a bit of .NET code that retrieves the results from an Oracle Stored Procedure, using the ADO.NET Library, and populates the results into a DataTable like so:
using System.Data.OracleClient;
public DataTable getData()
{
OracleConnection conn = new OracleConnection("Data Source=DATASOURCE;Persist Security Info=True;User ID=userID;Password=userPass;Unicode=True;Min Pool Size=1;Max Pool Size=20;Connection Lifetime=300");
DataTable dt = new DataTable();
conn.Open();
try
{
OracleCommand oraCmd = new OracleCommand();
oraCmd.Connection = conn;
oraCmd.CommandText = "stored_procedure.function_name";
oraCmd.CommandType = CommandType.StoredProcedure;
oraCmd.Parameters.Add("cursor", OracleType.Cursor).Direction = ParameterDirection.Output;
OracleDataAdapter oraAdapter = new OracleDataAdapter(oraCmd);
oraAdapter.Fill(dt);
}
finally
{
conn.Close();
return dt;
}
}
This code has been working without any issues on several projects I have implemented the code on. However I am running into an issue on a new project, where the Oracle DB machine is actually much slower to respond, and seems to become unresponsive when too many clients begin to access the hardware. What I would like to do is implement some sort of timeout on the oraAdapter.Fill command - as it appears that when the database becomes unresponsive, the .NET application will hang on the 'Fill' method for as long as 10 minutes or more, never reaching the 'finally' codeblock and closing the DB connection.
I am in an environment where I am restricted to using the MSDN Library for connecting to the Oracle Database, so I'm hoping I can do it using the ADO.NET Control.
The CommandTimeout property does not work using the System.Data.OracleClient .NET 3.5 Provider. It appears that this functionality is not supported without the use of an external library.
It seems you need the CommandTimeout property, not ConnectionTimeout.

Categories