一.
C#
语言的特点:
a)
通用,支持跨平台
b)
简单,主要体现垃圾回收、指代等特性上
c)
面向对象设计
d)
与
web
应用紧密的结合
e)
安全性机制
f)
兼容性
g)
灵活的版本处理技术
h)
C#
提供了完善的错误和异常触发机制
二.简单的
C#
编程语法:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
Sample1_1
//
定义的项目的命名空间
{
class
Program
//
定义了类
{
static
void
Main(
string
[] args)
//
定义的
main
函数
{
Console
.WriteLine(
"
请输入你的名字:
"
);
//
向屏幕输出
String
name =
Console
.ReadLine();
//
读取用户输入的数据
Console
.WriteLine(
"{0}
欢迎来到
C#4.0
世界
"
,name);
}
}
}
三.
C#
语言的变量、常量和数据类型:
a)
变量:变量数据类型
变量名(标示符)或者
变量数据类型
变量名(标示符)
=
变量值
b)
常量:
const
常量数据类型
常量名(标识符)
=
常量值
c)
数据类型:
C#
是一种强类型语言
i.
值类型:派生自
System.Object
的
System.ValueType
派生的,声明一个变量分
配在栈中
1.
简单类型:
sbyte
字节型
short
短整型
int
整型
long
长整型
float
浮点
型
double
双精度浮点型
char
字符型
bool
布尔型
2.
集合类型:用
enum
声明
3.
结构类型:用
struct
声明
ii.
引用类型:分配到堆中
1.
类类型:
2.
对象类型
3.
字符串类型:用
String
声明
4.
接口类型:用
interface
声明
namespace DatabaseCon
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string strCon = "";
private void Form1_Load(object sender, EventArgs e)
{
textBox6.Text = "(local)";
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*.mdb(Access数据库文件)|*.mdb|*.xls(Excel文件)|*.xls|*.*(所有文件)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.ShowDialog();
textBox6.Text = Form2.strServer;
}
private void button3_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
if (textBox1.Text != "")
{
FileInfo FInfo = new FileInfo(textBox1.Text);
string strExtention = FInfo.Extension;
if (strExtention.ToLower() == ".mdb")
{
if (textBox2.Text != "")
{
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox1.Text + ";UID=" + textBox2.Text + ";PWD=" + textBox3.Text + ";";
}
else
{
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox1.Text + ";";
}
}
else if (strExtention.ToLower() == ".xls")
{
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox1.Text + ";Extended Properties=Excel 8.0;";
}
}
OleDbConnection oledbcon = new OleDbConnection(strCon);
try
{
oledbcon.Open();
richTextBox1.Clear();
richTextBox1.Text = strCon + "\n连接成功……";
}
catch
{
richTextBox1.Text = "连接失败";
}
}
else if (radioButton2.Checked == true)
{
if (checkBox1.Checked == true)
{
strCon = "Data Source=" + textBox6.Text + ";Initial Catalog =" + comboBox1.Text + ";Integrated Security=SSPI;";
}
else if (checkBox2.Checked == true)
{
strCon = "Data Source=" + textBox6.Text + ";Database=" + comboBox1.Text + ";Uid=" + textBox5.Text + ";Pwd=" + textBox4.Text + ";";
}
SqlConnection sqlcon = new SqlConnection(strCon);
try
{
sqlcon.Open();
richTextBox1.Clear();
richTextBox1.Text = strCon + "\n连接成功……";
}
catch
{
richTextBox1.Text = "连接失败";
}
}
}