涨知识Style

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

示例1:

using System.Data.OleDb;

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

示例2:

 1 List<WordInfo>WordList = new List<WordInfo>();
 2 StreamReader sr = new StreamReader (filepath,Encoding.UTF8);
 3 string nextLine;
 4 while ((nextLine = sr.ReaLine()) !=null)
 5 {
 6       string[] strs = nextLine.Split('\t');
 7       WordInfo tmp = new  WordInfo ();
 8     tmp.word = strs[0];
 9     tmp.count = int.Parse(strs[1]);
10     WordList.Add(tmp);
11 }    
12     sr.Close();
13     int wordId = 1;
14     string insertstr = " ";
15     string updatestr = " ";
16     string connstr = "Provide=Microsoft.Jet.OLEDB.4.0 :Data Source=" +basepath:
17 
18 using (OleDbConnection conn = new OlsDbConnection(connst))
19 
20 {
21     try
22     {
23         conn.Open();
24         foreach (WordInfo tmp in WordList)
25         {
26                 insertstr = @"INSERT INTO WORDCOUNT WORD VALUES ('" +tmp.word+"')";
27                 updatestr @"UPDATE WORDCOUNT SET COUNT ="+tmp.count+"WHERE ID" ="+wordID;
28                  OleDbCommand insertcmd = new OleDbCommand(inserstr,conn);
29                    insercmd.ExecuteNonQuery();
30                    insercmd.Dispose();
31                 
32                    OleDbCommand updatecmd = new OlDeCommand(updatestr,conn);
33                    updatecmd.ExecuteNonQuery();
34                    updatecmd.Dispose();
35 
36                     wordId++;
37                     }
38                conn.Close();
39                  ...
40     

示例2

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();//非静态
}
}
public class MethodCall
{
    public static void main(String[] args)
    {
        Test.sayStatic();
        Test test = new Test();
        test.sayInstance();
    }
}
class Test
{
    public static void sayStatic()
    {
        System.out.println("这是一个静态方法。");
    }
    public void sayInstance()
    {
        System.out.println("这是一个实例方法。");
    }
}

 

3.打印沙漏图形

JAVA:
public class sdf {
    public static void main(String[] args) {
        int n = 7;
        int i,j,k,m,p;
        k = n/2+1;
        for (i = 0; i <= n; i++) {
            m = k - Math.abs(i-k);
             p = Math.abs(2*(k-i))-1;
            if(p == -1){
                continue;
            }
            for(j = 0 ; j < m; j ++){
                System.out.print(" ");
            }
            for(j = 0; j < p; j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

C#:
#include "stdio.h"
void main()
{
        int i,j,k; 
       
       
            for(i=1;i<=3;i++)
           {
            for(j=1;j<i;j++)
                   printf(" ");
            for(k=1;k<=7-2*i;k++) 
                   printf("*");
           printf("\n");
           }
            for(i=2;i<=3;i++)
        {
                    for(j=1;j<=3-i;j++)
                   printf(" ");
               for(k=1;k<=2*i-1;k++) 
                   printf("*");
              printf("\n");
        }
} 

SQL:

 

敬请期待!!!








posted @ 2016-03-08 16:18  白兴强  阅读(220)  评论(0编辑  收藏  举报