当前位置:编程学习 > C#/ASP.NET >>

这个C#的程序怎样理解啊?本人菜鸟,请大侠们帮我解释解释!谢谢啊

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace StudentMIS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //string studentno, studentname, studentsex;
            //int studentage;
            //studentno = textBox1.Text;
            //studentname = textBox2.Text;
            //studentsex = textBox3.Text;
            //studentage = int.Parse(textBox4.Text);

            //string sqlstr = "insert into studentinfo values('98002','李四','女',24)";
            //string sqlstr="delete from studentinfo where StudentSex='女'";
            string sqlstr = "update studentinfo set studentAge=studentAge+3 where studentName='张三'  ";
            
            try
            {
                string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
                strConnection += "Data Source=Student.mdb";
                using (OleDbConnection conn = new OleDbConnection(strConnection))
                {
                    OleDbCommand cmd = new OleDbCommand();
                    cmd.CommandText = sqlstr;
                    cmd.Connection = conn;
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("添加信息成功", "提示");
                    cmd.Dispose();
                    conn.Close();
                }

            }
            catch (System.Exception sqle)
            {
                MessageBox.Show(sqle.Message, "abc", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string studentno, studentname, studentsex;
            int studentage;
            studentno = textBox1.Text;
            studentname = textBox2.Text;
            studentsex = textBox3.Text;
            studentage = int.Parse(textBox4.Text);

           // string sqlstr = "insert into studentinfo values('98002','李四','女',24)";

            string sqlstr = "insert into studentinfo values('" + studentno + "',"+
                    "'"+studentname+"','"+studentsex+"',"+studentage+")";
                          

           // MessageBox.Show(sqlstr);
            try
            {
                string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
                strConnection += "Data Source=Student.mdb";
                using (OleDbConnection conn = new OleDbConnection(strConnection))
                {
                    OleDbCommand cmd = new OleDbCommand();
                    cmd.CommandText = sqlstr;
                    cmd.Connection = conn;
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("添加信息成功", "提示");
                    cmd.Dispose();
                    conn.Close();
                }

            }
            catch (System.Exception sqle)
            {
                MessageBox.Show(sqle.Message, "abc", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            }
        }
    }
} --------------------编程问答-------------------- 两个按钮,一个执行修改操作,一个执行插入操作 --------------------编程问答-------------------- button1:将张三的年龄加3
button2:新增一个学生 --------------------编程问答-------------------- 学习下........ --------------------编程问答-------------------- 谢谢哈,不过主要是这部分:

try
  {
  string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
  strConnection += "Data Source=Student.mdb";
  using (OleDbConnection conn = new OleDbConnection(strConnection))
  {
  OleDbCommand cmd = new OleDbCommand();
  cmd.CommandText = sqlstr;
  cmd.Connection = conn;
  conn.Open();
  cmd.ExecuteNonQuery();
  MessageBox.Show("添加信息成功", "提示");
  cmd.Dispose();
  conn.Close();
--------------------编程问答-------------------- 一个新增一个修改 --------------------编程问答-------------------- 看看数据库操作基础 --------------------编程问答--------------------
try
  {
  string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;"; 
  strConnection += "Data Source=Student.mdb";
  using (OleDbConnection conn = new OleDbConnection(strConnection)) //新建数据库连接
  {
  OleDbCommand cmd = new OleDbCommand();  //新建数据库命令
  cmd.CommandText = sqlstr;  //设置数据库命令
  cmd.Connection = conn;  //设置连接
  conn.Open();  //打开数据库连接
  cmd.ExecuteNonQuery();  //执行更新操作
  MessageBox.Show("添加信息成功", "提示");  //弹出提示框
  cmd.Dispose();  //销毁命令
  conn.Close();  //关闭连接,建议将这步放入finally

--------------------编程问答--------------------
引用 7 楼 sparkyu01 的回复:
C# code
try
  {
  string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;"; 
  strConnection += "Data Source=Student.mdb";
  using (OleDbConnection conn = new OleDbConnection(strConnection)) //……

说得够仔细的了! --------------------编程问答-------------------- 观摩观摩~ --------------------编程问答--------------------
C# code
try
  {
  string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;"; 
  strConnection += "Data Source=Student.mdb";//数据库连接字符串,一般的话还要加用户名和密码的,
  using (OleDbConnection conn = new OleDbConnection(strConnection)) //……
--------------------编程问答--------------------
引用 7 楼 sparkyu01 的回复:
C# code
try
  {
  string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;"; 
  strConnection += "Data Source=Student.mdb";
  using (OleDbConnection conn = new OleDbConnection(strConnection)) //……


这个说的很详细,之前你有一句我觉得不对,strConnection += "Data Source=Student.mdb";是指定服务器名,而不是指定数据库名吧,数据库名用database=Student.mdb;还有第一句是指定数据库提供程序,这里是System.Data.OleDb 
--------------------编程问答--------------------
引用 7 楼 sparkyu01 的回复:
C# code
try
  {
  string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;"; 
  strConnection += "Data Source=Student.mdb";
  using (OleDbConnection conn = new OleDbConnection(strConnection)) //……
--------------------编程问答-------------------- ADO.NET多多看看! --------------------编程问答-------------------- 一个新增一个修改 --------------------编程问答--------------------
using (OleDbConnection conn = new OleDbConnection(strConnection)) //新建数据库连接
{
   //
}


难不成是不理解这一段?   这是一种特定的结构。
用using包裹的conn 会在这个花括号块结束的时候自动调用conn的dispose()方法(如果有的话)。
conn可以替换为其它的对象。
主要是用于自动释放资源。 --------------------编程问答--------------------

新手求问。

try
{

}

cath
{

}
是什么意思? --------------------编程问答--------------------
引用 16 楼 woshierbiluanzi 的回复:
新手求问。

try
{

}

cath
{

}
是什么意思?



try
{
}
catch
{
}
吧??
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,