好吧,我创建了Visual Studio的C#(2012)和Access之间的连接。这意味着,我已经完成了手动方式(没有代码)。但是,这种方法无法帮助我完成我想要的功能,所以我也完成了编码方式。我的连接有问题C# - Microsoft Access 2010
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Configuration;
using System.Xml.Serialization;
namespace WindowsFormsApplication1
{
public partial class horaireForm : Form
{
DateTime semSess;
int numSemaine;
int jourSem;
int periode;
string theoOuLabo;
string lesCours;
string cours;
string lesProfs;
string unprof;
string lesLocaux;
string lesGroupes;
string laComm;
string conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Stanley\\Desktop\\stuff\\Hiver 2013\\Horaires_EcranA2013.accdb";
(对不起,如果它很长)。无论如何,在此之后,我在这里得到这个代码,它从组合框中获取信息(每个案例是大学学期的几周的日期)。由于显然我不能选择主键,如果它是组合框中的值,我决定使用日期作为组合框中选择的值,然后使用它来选择数据库中的实际主键。
semSess = Convert.ToDateTime(comboSemSess.Text);
OleDbConnection laConn = new OleDbConnection(conn);
try
{
laConn.Open();
laComm = "SELECT NumeroSemaine FROM SemainDelaSession WHERE DebutSemaine = " + semSess;
OleDbCommand myAccessCommand = new OleDbCommand(laComm, laConn);
OleDbDataReader reader = myAccessCommand.ExecuteReader();
while (reader.Read())
{
numSemaine = Convert.ToInt32(reader["NumeroSemaine"]);
}
}
catch
{
MessageBox.Show("Une erreur s'est produite en accédant à la base de données");
}
finally
{
laConn.Close();
}
但是,我仍然无法连接。它仍然获取MessageBox。这是否与我之前做过另一个连接,或者我键入connectionString的方式有关,因为我不知道。
你为什么不抓住例外,看看它有什么说法? catch(Exception ex){MessageBox.Show(ex.ToString())}并且你将会知道什么是错误的。 – lauCosma
感谢您的回答。我完全忘了你可以这样做。无论如何,我试图做到这一点......当我尝试将“semSess”分配给“DebutSemaine”时,它会出现语法错误。说像“DebutSemaine = 2013-02-25 00:00:00”。 –