We have a existing closed source third party application using a Pervasive PSQL database. For Example the PSQL are located in the directory c:\test and have names like holiday.dat, offers.dat and so on. I want to read and if possible write to these files without having installed the Pervasive Workstation Engine. With the Workstation Engine and an ODBC connection it runs without any problems. But we won't install the Workstation Engine on any client and the third party application doesn't it, too.
On connectionsstrings.com i found the connection string:
"Provider=PervasiveOLEDB;Data Source=C:\datafilesDirectory;"
using directives:
using Pervasive.Data.SqlClient;
using System.Data.OleDb;
using System.Xml.Serialization;
test connection snippet:
string strAccessConn = "Provider=PervasiveOLEDB;Data Source=C:\datafilesDirectory;"
string strAccessSelect = "SELECT * FROM holidays";
// Create the dataset and add the Categories table to it:
DataSet myDataSet = new DataSet();
OleDbConnection myAccessConn = null;
try
{
myAccessConn = new OleDbConnection(strAccessConn);
}
catch(Exception ex)
{
Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
return;
}
try
{
OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect,myAccessConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
myAccessConn.Open();
myDataAdapter.Fill(myDataSet,"Categories");
}
catch (Exception ex)
{
Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message);
return;
}
finally
{
myAccessConn.Close();
}
The application can't open the database connection.
In the connectionstring, you should replace C:\datafilesDirectory; with C:\test;.
Related
I have to check for database availability using c# scripting for an "oledb" connection in an ssis package.
Since my connection is oledb I'm not able to use AcquireConnection method.
I tried using ConnectionString property of ConnectionManager but I get the connection string even when database is offline.
Any idea how to go about with unmanaged Objects here oledb.
You can find the complete documentation here https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/ado-net-code-examples
what you may need to test the oledb connection is then something like this:
string connectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ "c:\\Data\\Northwind.mdb;User Id=admin;Password=;";
OleDbConnection connection = new OleDbConnection(connectionString);
try
{
connection.Open();
connection.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return true;
I have a mssql database on remote machine and backup file for this database. I want to create a method (c#) for restoring of database from backup. I will execute my method on my local machine. Can somebody help me to create such method for restoring remote database?
Try the this:
public void RestoreDatabase(string fileName)
{
try
{
using (SqlConnection conn = new SqlConnection("connectionString"))
{
string sql = "RESTORE DATABASE YourDatabase FROM DISK = N''" + fileName;
conn.Open();
SqlCommand _command = new SqlCommand(sql, conn);
_command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
throw;
}
}
You call it this way :
RestoreDatabase(#"\\remotemachine\...\YourFile.bak");
NB: Put an actual path for where the file is located
I've searching for this and I thought I found the answer on here. this is the code I found to run a sql script through c#:
using System.Data.SqlClient;
using System.IO;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
namespace SeleniumTest2
{
class CreateSchema
{
public void Schema_Create()
{
string sqlConnectionString = "connection string here";
FileInfo file = new FileInfo(#"filepath to script.sql");
string script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
//DOESNTLIKE
server.ConnectionContext.ExecuteNonQuery(script);
file.OpenText().Close();
conn.Close();
}
}
}
But I keep getting the following error:
An unhandled exception of type 'Microsoft.SqlServer.Management.Common.ExecutionFailureException' occurred in Microsoft.SqlServer.ConnectionInfo.dll
Additional information: An exception occurred while executing a Transact-SQL statement or batch.
Can anyone tell me how to overcome this error?
THANKS!!
This happened to me a couple times. After debugging, there were some errors in my script file itself. The following worked for me:
Try running your script file directly using SQL Management Studio. This can pinpoint errors in your script itself.
Break down the SQL script into smaller files. For some reason this worked for me. Split the file into smaller scripts accordingly. For my particular database creation script, I separated it into a create tables script, a populate tables script, and an add primary and foreign keys script.
My code:
/// <summary>
/// Process SQL script with "GO" statements
/// </summary>
/// <param name="script"></param>
public static void ProcessSQLScriptFile(string script)
{
try
{
SqlConnection con = new SqlConnection(Properties.Settings.Default.SQLConDefault); // your connection string
con.Open();
Server server = new Server(new ServerConnection(con));
server.ConnectionContext.ExecuteNonQuery(script);
con.Close();
}
catch (SqlException e)
{
Console.WriteLine("SQL Exception: " + e.Message);
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
}
Hope this helps.
You may try this method to execute sql (from msdn):
private static void ExecuteCommand(string queryString,
string connectionString)
{
using (SqlConnection connection = new SqlConnection(
connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
}
If you will get an error, check exception details, check if your connection string is valid.
I had this exact same issue. What fixed it for me was to find out the actual error:
public void Schema_Create()
{
string sqlConnectionString = "connection string here";
FileInfo file = new FileInfo(#"filepath to script.sql");
string script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
try
{
server.ConnectionContext.ExecuteNonQuery(script);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.InnerException.Message);
}
file.OpenText().Close();
conn.Close();
}
My issue presented itself in the ex.InnerException.Message, which in my case happened to be that my script was attempting to write a column that already existed on the table (column names must be unique for a given table).
Change the database path to another drive
Because in c drive or in windows drive you don't have permission to create data base
If change the path , your solution is work successful.
Hi I'm having a problem finding the correct connection statement for my web-service to an sql-server database. I'm trying to retrieve data from my database to check a users login details.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace BTC_Service
{
public class UseDatabase
{
SqlConnection sqlConn;
internal Boolean Connect()
{
try
{
sqlConn = new SqlConnection(#"Integrated Security=true; Initial Catalog=BTCFS_DataBase; Data Source=.\SQLEXPRESS;");
sqlConn.Open();
return true;
}
catch (SqlException ex)
{
return false;
}
}
internal void DisconnectDatabase()
{
sqlConn.Close();
}
internal Boolean ExecuteCommand(String query)
{
try
{
SqlCommand cmd = sqlConn.CreateCommand();
cmd.CommandText = query;
cmd.ExecuteNonQuery();
return true;
}
catch (SqlException ex)
{
return false;
}
}
internal SqlDataReader ExecuteQuery(String query)
{
try
{
SqlCommand cmd = sqlConn.CreateCommand();
cmd.CommandText = query;
return cmd.ExecuteReader();
}
catch (SqlException ex)
{
return null;
}
}
}
}
The database is created with sql-server 2008 and the path for it is:
C:\BTCFS_DataBase\db_BTDC_data.mdf
and the log file
C:\BTCFS_DataBase\db_BTDC_log.ldf
There is no password for the database and the code is as follows:
USE master
GO
create database db_BTCFC
ON PRIMARY
(
NAME = 'db_BTCFC_Data',
FILENAME = 'c:\BTCFS_DataBase\db_BTDC_data.mdf',
SIZE = 5MB,
FILEGROWTH = 10%
)
LOG ON
(
NAME = 'db_BTFC_log',
FILENAME = 'c:\BTCFS_DataBase\db_BTDC_log.ldf',
SIZE = 5MB,
FILEGROWTH = 10%
)
GO
Is there any suggestion to what I am doing wrong?
Should I add the database to visual studio in a specific way?
Or am i creating my database in the wrong way?
Thank you in advance.
The fact that you are connected using Integrated Security, means that your local user account on Windows should be authenticated on the SQL server instance which is hosted locally on your machine (evident by the "." in the Data Source, which refers to your local machine). It might be that the setup of your SQL server instance doesn't accommodate windows authentication. Check that your configuration allows for "mixed mode" authentication, i.e. either Windows authentication or username/password authentication...
I found this statement to be more effective than the previous one:
sqlConn = new SqlConnection(#"Integrated Security=SSPI; Initial Catalog=BTCFS_DataBase; Data Source=localhost");
Thanks #Wolfish for the link.
I have created ODBS user DNS for database, opened VS, created DataSet and imported one table members. I would like to read all records from dataset, how to do that? I have tried query below but it return no result. I can preview data using preview menu in designer but do not find a way to get data using code.
var dataSet = new DataSet1();
var membersDataTable = dataSet.members;
var take = membersDataTable.Take(100);
It looks like you have created the schema for a DataSet, but you have not run any queries to load the DataSet.
using (OdbcConnection connection =
new OdbcConnection(connectionString))
{
string queryString = "SELECT * FROM Members";
OdbcDataAdapter adapter =
new OdbcDataAdapter(queryString, connection);
// Open the connection and fill the DataSet.
try
{
connection.Open();
adapter.Fill(dataSet);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
// The connection is automatically closed when the
// code exits the using block.