See the following for a proper connection string and would recommend using OdbcConnectionStringBuilder.
Cannot convbert from system.data.odbc/odbcConnection to string c#

i keep getting an error saying it can't convert to string and saying and cant understand why,
private void InitializeDataGridView()
{
try
{
// Set up the DataGridView.
dataGridView1.Dock = DockStyle.Fill;
// Automatically generate the DataGridView columns.
dataGridView1.AutoGenerateColumns = true;
// Set up the data source.
bindingSource1.DataSource = GetData("Select * From shead");
dataGridView1.DataSource = bindingSource1;
// Automatically resize the visible rows.
dataGridView1.AutoSizeRowsMode =
DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
// Set the DataGridView control's border.
dataGridView1.BorderStyle = BorderStyle.Fixed3D;
// Put the cells in edit mode when user enters them.
dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
}
catch (SqlException)
{
MessageBox.Show("To run this sample replace connection.ConnectionString" +
" with a valid connection string to a Northwind" +
" database accessible to your system.", "ERROR",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
System.Threading.Thread.CurrentThread.Abort();
}
}
private static DataTable GetData(string sqlCommand)
{
System.Data.Odbc.OdbcConnection connection =
new System.Data.Odbc.OdbcConnection("Driver={Microsoft Paradox Driver (*.db )};"
+ "DriverID=538;Fil=Paradox .X;"
+ "DefaultDir=C:\\Retcon\\Shifts\\;"
+ "Dbq=C:\\Retcon\\Shifts\\;"
+ "CollatingSequence=ASCII;");
System.Data.Odbc.OdbcCommand command =
new System.Data.Odbc.OdbcCommand();
using (SqlConnection northwindConnection = new SqlConnection(connection))
{
using (SqlCommand command = new SqlCommand(sqlCommand, northwindConnection))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
adapter.Fill(table);
return table;
}
}
}
but i get the following errors,
any help would be much appreciated as at a total loss to why its doing this.
3 answers
Sort by: Most helpful
-
Bruce (SqlWork.com) 31,816 Reputation points
2022-08-21T16:07:46.61+00:00 SqlConnection(connection) takes a string, not an OdbcConnection object.
As the code only uses the SqlConnection, SqlCommand, it is not clear why you create an OdbcConnection() and OdbcCommand(). These are different libraries.
SqlConnection() and SqlCommand() only support SqlServer queries.
Jack J Jun 22,381 Reputation points Microsoft Vendor2022-08-23T07:31:55.647+00:00 @mion shion , Welcome to Microsoft Q&A, as others suggested, we need to use the same type of connection to get datatable from sql database.
You could try the following code to get the datatable by using the command.
private static DataTable GetData(string sqlCommand) { System.Data.Odbc.OdbcConnection connection = new System.Data.Odbc.OdbcConnection("Driver={Microsoft Paradox Driver (*.db )};" + "DriverID=538;Fil=Paradox .X;" + "DefaultDir=C:\\Retcon\\Shifts\\;" + "Dbq=C:\\Retcon\\Shifts\\;" + "CollatingSequence=ASCII;"); System.Data.Odbc.OdbcCommand command = new System.Data.Odbc.OdbcCommand(sqlCommand,connection); System.Data.Odbc.OdbcDataAdapter adapter = new System.Data.Odbc.OdbcDataAdapter(); adapter.SelectCommand = command; DataTable table = new DataTable(); adapter.Fill(table); return table; }
Hope this could help you.
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.