I am working with C# windows application
and I am facing a problem with OLEDB connection to SQL SERVER 2008
my code is too simple:
I am trying to fill the datagridview from this query
string connString = "Provider=SQLOLEDB;Data Source=servername;Initial Catalog=DBname;Integrated Security=SSPI";
string query = "SELECT * FROM account";
//create an OleDbDataAdapter to execute the query
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
//create a command builder
OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);
//create a DataTable to hold the query results
DataTable dTable = new DataTable();
//fill the DataTable
dAdapter.Fill(dTable);
//the DataGridView
//DataGridView dgView = new DataGridView();
//BindingSource to sync DataTable and DataGridView
BindingSource bSource = new BindingSource();
//set the BindingSource DataSource
bSource.DataSource = dTable;
//set the DataGridView DataSource
dataGridView_FraudDetails.DataSource = bSource;
dAdapter.Update(dTable);
but I get the Following error in this line
//fill the DataTable
dAdapter.Fill(dTable);
"[DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied."
The code was running well , but when I uninstalled the server and reinstalled it again it gave me that error
I tried to turn off the firewall but it doesn't work
any suggestion please
Are you using SQL Server Express? If so you need to make sure that it is configured to accept connections via either TCP/IP or named pipes. By default, SQL Server Express does not accecpt connections. See http://www.datamasker.com/SSE2005_NetworkCfg.htm (this page is specific to SQL Server 2005 but should apply to 2008 as well).
Go to SQL Server Configuration Manager (under All Programs > Microsoft SQL Server in start menu) and for your instance, make sure TCP/IP is enabled. It is disabled by default for a new install of SQL Server.
Related
I am creating a C# WinForms Application along with SQL LocalDB. I am new to SQL and I am following this tutorial (timestamp: 5.50) to add database in project. I have also added a setup project to install the application to other PCs. When I installed the project for the first time it worked perfectly well, but when I updated the database schema (added one more column) the changes are not reflecting, updated version of setup & assembly and installed the setup file again, the database schema changes are not reflecting in the application. It is giving the following error:
My code is something like this:
private void load_list()
{
string conn = Properties.Settings.Default.dbAgeConnectionString;
SqlConnection sqlConn = new SqlConnection(conn);
string sqltext = "SELECT Name, Age, DOB from Users";
if (sqlConn.State != ConnectionState.Open) sqlConn.Open();
DataTable table = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(sqltext,sqlConn);
adapter.Fill(table);
sqlConn.Close();
lstRecords.DisplayMember = "Name";
lstRecords.ValueMember = "Id";
lstRecords.DataSource = table;
gvrecords.DataSource = table;
}
Connection String:
Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\dbAge.mdf;Integrated Security=True
I tried adding the database files manually in Application folder of Setup but still no results.
I searched on the internet but did not find any database version control or upgrade method for SQL LocalDB. Is there any way I can achieve this?
Thanks in advance.
I am trying to make a database application. I added local database from
add > new item > local database.sdf
In Server Explorer, I created a table in the database. But I am having trouble connecting to it.
I want to show all the data in a DataGrid.
My code:
string ConnectionString = #"Data Source=""c:\users\asus\documents\visual studio 2012\Projects\WpfApplicationLocalDB\WpfApplicationLocalDB\LocalDB.sdf""";
SqlConnection conn = new SqlConnection(ConnectionString);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Student", conn);
DataTable dt = new DataTable();
da.Fill(dt);
conn.Close();
List<DataRow> lis = dt.AsEnumerable().ToList();
DataGridView.ItemsSource = lis;
But when I build it, Visual Studio finds conn.open(); error. A message says that
SqlException was unhandled by user
Please help...
Also, can anyone suggest a tutorial of how can I create a simple database application in C#? Please help.
If you're using a .sdf file, you're using Microsoft SQL Server Compact Edition (SQL Server CE).
When using SQL Server CE, you must use SqlCeConnection and SqlCeCommand classes - not SqlConnection and SqlCommand (those are for the "full", server-based versions of SQL Server)
Firstly, For Those who would like to ask WHY on earth am I DOWNSIZING from SQL SERVER to ACCESS, let me tell you the scenario.
There are some PC's with very low configuration, (256 MB RAM, 2GHzProcessor) I cannot install SQL Server. Hence I want major operations to carry out on SQL server and some data retrieving and auditing work to be done on Slower machine.
So here we go:
I want to copy table from SQL Server to MS Access 2007. I tried:
1)Connect to sql server, fill a datatable object by reading table from sql server.
2) Create a connection to MS Access, and use Dataadapter.Update method to update table to MS Access database.
However 2nd step is not working although its not throwing any error. Here is my code:
SqlConnection cnn = new SqlConnection(#"initial catalog=DBTempleERM;user id=aditya;password=Aditya_ravi$;Data Source=adityalappy\sqlexpress");
SqlCommand cmd = new SqlCommand("SELECT * FROM donationdetails", cnn);
cnn.Open();
System.Data.SqlClient.SqlDataAdapter sDA = new SqlDataAdapter(cmd);
DataTable donationdetails = new DataTable();
sDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
sDA.Fill(donationdetails);
MessageBox.Show(donationdetails.Rows.Count.ToString());
OleDbConnection oleConn = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Jet OLEDB:Database Password=Aditya_ravi$;Data Source=C:\dbt.accdb");
oleConn.Open();
OleDbCommand oleComm = new OleDbCommand();
OleDbDataAdapter oDA = new OleDbDataAdapter(oleComm);
OleDbCommandBuilder oCb = new OleDbCommandBuilder(oDA);
oDA.Update(donationdetails);
No error is thrown at the end of the execution, but I cannot see any records copied from SQL Server to MS Access.
I learnt that SQL Bulk copy cannot be used to copy from SQL Server to Access.
I also want to add the primary key from SQL Server table to MS Access table.
Why dont you use SSIS to do this for you.
You can create a SSIS package to copy a sql table to MS access.
If you want to initiate by .NET then create a SSIS package and call it from .NET
For details
At this point, oDA is not connected to any table on the Access side:
oDA.Update(donationdetails);
So even though you have all the data in a DataTable, you haven't got a target to copy it into.
I don't think this is the best approach, but that's the core of why your code isn't working as it is.
Ancient question, but I'm betting the RowState of all your rows in donationdetails were Unchanged, so the DataAdapter treats them as "I don't need to do anything with this row."
You can use dataset object instead of datatable.
DataSet ds=new DataSet();
sDA.Fill(ds,tablename);
oDA.Update(ds);
I am using C#, .net 3.5 and a MySQL-Database. I have a populated table on Server 1 which I want to copy to Server 2. On Server 2 I have the same table structure, but the table is empty. Now I want to copy the data from Server 1 to Server 2.
I connect to Server 1 and fill the information into a DataSet - no problem. Then I open a second connection to the other server. My problem is, how can I store this DataSet on the second Server? The Update()-command has no effect, even if I set the same UpdateCommand und InsertCommand-CommandText as for Server 1. I get no error when I use Update(DataSet,"TableName"), but the table is still empty.
For MSSQL-Databases BulkCopy would be an option, but it seems that there is no equivalent for MySQL DBs!?
I do not want to use mysqldump, I want to do it programmaticaly in C# on a client.
Any idea?
EDIT:
MySqlConnection conn_DB1 = new MySqlConnection(connString_DB1);
MySqlDataAdapter adp_DB1 = new MySqlDataAdapter("select * from myDB", conn_DB1);
DataSet theDataSet_DB1 = new DataSet();
adp_DB1.Fill(theDataSet_DB1, "myDB"); //everything is fine, the Data is there
MySqlConnection conn_DB2 = new MySqlConnection(connString_DB2);
MySqlDataAdapter adp_DB2 = new MySqlDataAdapter("select * from myDB", conn_DB2);
DataSet theDataSet_DB2 = new DataSet();
adp_DB1.Fill(theDataSet_DB2, "myDB"); //this DataSet is empty, of course
theDataSet_DB2 = theDataSet_DB2.Copy(); //the data is updated, the second DataSet has all the rows as expected
adp_DB2.Update(theDataSet_DB2, "myDB"); //no error on execution, but the table is still empty on the second server
The rows in theDataSet_DB2.Tables are "unchanged", so any occurs. You must mark rows as "added" before to update them.
foreach (DataTable table in theDataSet_DB2.Tables)
foreach (DataRow row in table.Rows)
row.SetAdded ();
It's necessary to build a MySqlCommandBuilder associated to the dataAdapter:
new MySqlCommandBuilder (dataAdapter);
I am not sure which library you use to access MySQL DB...
For a "pure ADO.NET" with the original ADO.NET provider from MySQL see this artcile http://www.codeproject.com/KB/database/GenericCopyTableDataFcn.aspx
IF you use the MySQL .NET connector there is a class called MySqlBulkLoader see http://dev.mysql.com/doc/refman/5.1/en/connector-net-programming-bulk-loader.html . this class takes AFAIK a file - so would have to create a file from the source table first to use it...
IF you are using the Devart MySQL components you could:
MySqlLoader ML = new MySqlLoader("myDB", conn_DB2);
ML.CreateColumns();
ML.LoadTable(theDataSet_DB1.Tables[0]);
I want to be able to edit a table in a SQL server database using c#.
Can someone please show me a very simple tutorial on connecting to the DB and editing data in a table.
Thank you so much.
First step is to create a connection. connection needs a connection string. you can create your connection strings with a SqlConnectionStringBuilder.
SqlConnectionStringBuilder connBuilder = new SqlConnectionStringBuilder();
connBuilder.InitialCatalog = "DatabaseName";
connBuilder.DataSource = "ServerName";
connBuilder.IntegratedSecurity = true;
Then use that connection string to create your connection like so:
SqlConnection conn = new SqlConnection(connBuilder.ToString());
//Use adapter to have all commands in one object and much more functionalities
SqlDataAdapter adapter = new SqlDataAdapter("Select ID, Name, Address from myTable", conn);
adapter.InsertCommand.CommandText = "Insert into myTable (ID, Name, Address) values(1,'TJ', 'Iran')";
adapter.DeleteCommand.CommandText = "Delete From myTable Where (ID = 1)";
adapter.UpdateCommand.CommandText = "Update myTable Set Name = 'Dr TJ' Where (ID = 1)";
//DataSets are like arrays of tables
//fill your data in one of its tables
DataSet ds = new DataSet();
adapter.Fill(ds, "myTable"); //executes Select command and fill the result into tbl variable
//use binding source to bind your controls to the dataset
BindingSource myTableBindingSource = new BindingSource();
myTableBindingSource.DataSource = ds;
Then, so simple you can use AddNew() method in the binding source to Add new record and then save it with update method of your adapter:
adapter.Update(ds, "myTable");
Use this command to delete a record:
myTableBindingSource.RemoveCurrent();
adapter.Update(ds, "myTable");
The best way is to add a DataSet from Project->Add New Item menu and follow the wizard...
Assuming you're using Visual Studio as your IDE you could just use LINQ to SQL. It's a pretty simple way to interact with your database and it should be pretty quick to get going.
Using LINQ to SQL is a pretty simple walk through in getting it up and running.
Have a read of the MSDN tutorial on Creating Data Applications. You may be able to clarify your question, or find the answers you need.
There is info on editing the data in the app but you have to get connected and load it into your app first.
The only reason to do this in C# is if you want to automate it somehow or create an interface for non-technical users to interact with the database. You can use a GridView control with an SQL datasource to manipulate the data.
#kevin: if he's just learning, I think its probably simpler to have him use SQLCommand object (or SQLDataAdapter).