如何在Visual Studio中使用C#代码打开和关闭与服务器资源管理器中的数据库(.db)数据连接的连接?
下面的代码是正在编写的应用程序的数据访问层。该代码有两种方法,使用如何在Visual Studio中使用C#代码打开和关闭与服务器资源管理器中的数据库(.db)数据连接的连接?,c#,.net,dataset,odbc,sqlanywhere,C#,.net,Dataset,Odbc,Sqlanywhere,下面的代码是正在编写的应用程序的数据访问层。该代码有两种方法,使用OdbcConnection和OdbcDataAdapter尝试两次连接到.db数据库文件,该文件在本地文件路径中设置并引入Visual Studio Server Explorer数据连接: using System.Data; using System.Data.Common; using System.Data.Odbc; using System.Windows.Forms; namespace Data.DataAcc
OdbcConnection
和OdbcDataAdapter
尝试两次连接到.db数据库文件,该文件在本地文件路径中设置并引入Visual Studio Server Explorer数据连接:
using System.Data;
using System.Data.Common;
using System.Data.Odbc;
using System.Windows.Forms;
namespace Data.DataAccess
{
public class PatientDatabase
{
public OdbcDataAdapter ConnectAndCreateDataAdapter()
{
string selectCommand = "select * from patient";
var connectionString = "Driver={SQL Anywhere 10};DBN=Training1;UID=dba;PWD=sql;Data Source=C:\\Projects\\Training\\Training1.4.2\\Training1.db;";
OdbcConnection connection = new OdbcConnection(connectionString);
connection.Open();
var dataAdapter = new OdbcDataAdapter(selectCommand, connectionString);
return dataAdapter;
}
public void ConnectAndPopulateDataGridView(DataGridView dataGridView)
{
BindingSource patientBindingSourceTest = new BindingSource();
dataGridView.DataSource = patientBindingSourceTest;
string selectCommand = "SELECT patient.first_name + ' ' + patient.last_name as patient_name, patient.patient_id, patient.address, patient.city, patient.state, patient.zipcode, patient.phone, patient.notes, patient.classification_id, patient_classification.description from patient, patient_classification right join patient on patient.classification_id = patient_classification.patient_classification_id";
var connectionString = "Driver={SQL Anywhere 10};DBN=Training1;UID=dba;PWD=sql;Data Source=C:\\Projects\\Training\\Training1.4.2\\Training1.db;";
OdbcConnection connection = new OdbcConnection(connectionString);
connection.Open();
var dataSet = new DataSet();
var dataAdapter = new OdbcDataAdapter(selectCommand, connectionString);
DataTableMapping patientTableMapping = new DataTableMapping("Table", "patient");
dataAdapter.TableMappings.Add(patientTableMapping);
patientTableMapping.ColumnMappings.Add(new DataColumnMapping("patient_id", "Patient ID"));
patientTableMapping.ColumnMappings.Add(new DataColumnMapping("patient_name", "Patient Name"));
patientTableMapping.ColumnMappings.Add(new DataColumnMapping("address", "Address"));
patientTableMapping.ColumnMappings.Add(new DataColumnMapping("city", "City"));
patientTableMapping.ColumnMappings.Add(new DataColumnMapping("state", "State"));
patientTableMapping.ColumnMappings.Add(new DataColumnMapping("zipcode", "Zip Code"));
patientTableMapping.ColumnMappings.Add(new DataColumnMapping("phone", "Phone"));
patientTableMapping.ColumnMappings.Add(new DataColumnMapping("notes", "Notes"));
patientTableMapping.ColumnMappings.Add(new DataColumnMapping("classification_id", "Classification ID"));
patientTableMapping.ColumnMappings.Add(new DataColumnMapping("description", "Classification"));
dataAdapter.Fill(dataSet);
patientBindingSourceTest.DataSource = dataSet.Tables["patient"].DefaultView;
dataGridView.Columns["Notes"].Visible = false;
dataGridView.Columns["Patient ID"].Visible = false;
dataGridView.Columns["Classification ID"].Visible = false;
var commandBuilder = new OdbcCommandBuilder(dataAdapter);
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
}
}
现在的问题是,这段代码工作和功能都非常好。。。但是
打开解决方案时,数据连接(数据库)开始关闭。上面的图片链接显示了我所指的内容
问题是,除非连接“打开”,否则我的代码无法工作
connection.Open()代码>不会打开该连接,即使它们在创建时具有相同的连接字符串。打开连接包括手动右键单击数据连接中列出的数据库,然后单击“刷新”
所以
是否有任何方法可以通过代码完成此右键单击?我可以像打开连接时那样打开连接吗。。。SQL Server?否则,当我编译并发布代码时会发生什么?数据库是否在应用程序中构建并响应代码connection.Open()代码>或当数据适配器打开连接时?只需将数据库文件放入启动项目文件夹的bin\debug中,并将连接路径更改为“DBF=Training1.db;UID=dba;PWD=sql”,我的坏消息。很抱歉,在我解决问题后,花了这么长时间才发布答案。只需将数据库文件放在启动项目文件夹的bin\debug中,并将连接路径更改为“DBF=Training1.db;UID=dba;PWD=sql”,我的错误。很抱歉,在我解决问题后,我花了这么长时间才发布答案