用Access作为后台数据库支撑,书写一个C#写入记录的案例

示例1:

1 using System.Data.OleDb;
2 
3 string sql = "insert into 表 (列1,列2,列3) values('"+comboBox1.Text+"','"+comboBox1.Text+"','"+textBox1.Text+"')"; //构造sql语句
4 string dbpath = AppDomain.CurrentDomain.BaseDirectory + "\data\database1.mdb";//access数据库的路径
5 OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + dbpath); //定义数据库连接对象
6 OleDbCommand cmd = new OleDbCommand(sql, con); //定义Command对象
7 con.Open(); //打开数据库连接
8 cmd.ExecuteNonQuery(); //执行Command命令
9 con.Close(); //关闭数据库连接

示例2:

List<WordInfo>WordList = new List<WordInfo>();
StreamReader sr = new StreamReader (filepath,Encoding.UTF8);
string nextLine;
while ((nextLine = sr.ReaLine()) !=null)
{
      string[] strs = nextLine.Split('	');
      WordInfo tmp = new  WordInfo ();
    tmp.word = strs[0];
    tmp.count = int.Parse(strs[1]);
    WordList.Add(tmp);
}    
    sr.Close();
    int wordId = 1;
    string insertstr = " ";
    string updatestr = " ";
    string connstr = "Provide=Microsoft.Jet.OLEDB.4.0 :Data Source=" +basepath:

using (OleDbConnection conn = new OlsDbConnection(connst))

{
    try
    {
        conn.Open();
        foreach (WordInfo tmp in WordList)
        {
                insertstr = @"INSERT INTO WORDCOUNT WORD VALUES ('" +tmp.word+"')";
                updatestr @"UPDATE WORDCOUNT SET COUNT ="+tmp.count+"WHERE ID" ="+wordID;
                 OleDbCommand insertcmd = new OleDbCommand(inserstr,conn);
                   insercmd.ExecuteNonQuery();
                   insercmd.Dispose();
                
                   OleDbCommand updatecmd = new OlDeCommand(updatestr,conn);
                   updatecmd.ExecuteNonQuery();
                   updatecmd.Dispose();

                    wordId++;
                    }
               conn.Close();
                 ...

示例3:

count是access中的保留字,这类保留字是不建议用作字段名表名之类的(更多内容你可以搜索一下 access保留字),如果是象你图上一样,你在字段名上用了保留字count,可以尝试一下用中括号将字段名括起来,比如update wordcount set [count]=
2.关于一个类中方法调用种种情况


如果该方法声明为static,则直接使 用类名.方法名() 即可,
不为static就要实例化该类,再用 实例对象.方法名() 调用。
在当前类调用当前类的方法,直接方法名()
如果另一个类中的那个方法是私有的话,就不能直接调用到,
如果是其他类型的话看情况,
如果是非静态的,就需要利用另一个类的实例(也就是用那个类生成的对象)来调用。

如:

class A{
public static void a(){}
public void b(){}

}

public class B{
public static void main(String[] args){
A.a();//静态

new A().b();//非静态
}
}
 1 public class MethodCall
 2 {
 3     public static void main(String[] args)
 4     {
 5         Test.sayStatic();
 6         Test test = new Test();
 7         test.sayInstance();
 8     }
 9 }
10 class Test
11 {
12     public static void sayStatic()
13     {
14         System.out.println("这是一个静态方法。");
15     }
16     public void sayInstance()
17     {
18         System.out.println("这是一个实例方法。");
19     }
20 }
原文地址:https://www.cnblogs.com/1And0/p/5257304.html