c#操作sql server2008 的界面实例代码

先是查询整张表,用到combobox选择查询哪张表,最后用DataGridView显示

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
    }
    private void Form1_Load(object sender, EventArgs e)
    {
      this.dataGridView1.RowHeadersVisible = false;
      this.dataGridView1.AllowUserToAddRows = false;
      this.dataGridView1.ReadOnly = true;
      this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
      // this.comboBox1.SelectedIndex =0;
      string sql = "select * from student";
      DataTable table = SqlManage.TableSelect(sql);
      this.dataGridView1.DataSource = table;
      comboBox1.Items.Add("学生表");
      comboBox1.Items.Add("教师表");
    }
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
      string sql = "";
      switch (this.comboBox1.SelectedIndex)
      {
        case 0:
          sql = "select id as 学生号,name as 姓名,sage as 年龄 from student";
          break;
        case 1:
          sql = "select t_id as 教师号,t_name as 姓名,T_age as 年龄 from teacher";
          break;
        default:
          break;
      }
      DataTable table = SqlManage.TableSelect(sql);
      this.dataGridView1.DataSource = table;
    }
  }
} 

然后是修改表格,这个比较简单,用到textbox和button

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
  public partial class Form2 : Form
  {
    public Form2()
    {
      InitializeComponent();
    }
    private void button4_Click(object sender, EventArgs e)
    {
      this.Close();
    }
    private void button1_Click(object sender, EventArgs e)
    {
      string sql = string.Format("insert into teacher values('{0}','{1}','{2}')",
              this.textBox1.Text, this.textBox2.Text, this.textBox3.Text);
      SqlManage.TableChange(sql);
    }
    private void button2_Click(object sender, EventArgs e)
    {
      string sql = string.Format("update teacher set ('{0}',''{1}'','{2}')",
              this.textBox1.Text, this.textBox2.Text, this.textBox3.Text);
      SqlManage.TableChange(sql);
    }
    private void button3_Click(object sender, EventArgs e)
    {
      string sql = string.Format("delete from teacher where t_id='{0}'",
              this.textBox1.Text);
      SqlManage.TableChange(sql);
    }
    private void Form2_Load(object sender, EventArgs e)
    {
    }
  }
} 

按条件查询表格,这个是核心,用到radiobutt,combobox,,button, DataGridView

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
  public partial class Form3 : Form
  {
    public Form3()
    {
      InitializeComponent();
    }
    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
    }
    private void Form3_Load(object sender, EventArgs e)
    {
      this.comboBox1.Enabled = false;
      this.comboBox2.Enabled = false;
      this.comboBox3.Enabled = false;
      this.comboBox4.Enabled = false;
      //初始化教师编号
      string sql = "select t_id from teacher";
      DataTable table = SqlManage.TableSelect(sql);
      string t_id;
      foreach (DataRow row in table.Rows)
      {
        t_id = row["t_id"].ToString();
        this.comboBox1.Items.Add(t_id);
      }
      if (table.Rows.Count > 0)
      {
        this.comboBox1.SelectedIndex = 0;
      }
      //初始化教师姓名
      string sql_name = "select t_name from teacher";
      table.Clear();
      table = SqlManage.TableSelect(sql_name);
      string t_name;
      foreach (DataRow row in table.Rows)
      {
        t_name= row["t_name"].ToString();
        this.comboBox2.Items.Add(t_name);
      }
      if (table.Rows.Count > 0)
      {
        this.comboBox2.SelectedIndex = 0;
      }
      //初始化学生
      string sql_id = "select id from student";
      table.Clear();
      table = SqlManage.TableSelect(sql_id);
      string s_id;
      foreach (DataRow row in table.Rows)
      {
        s_id = row["id"].ToString();
        this.comboBox3.Items.Add(s_id);
      }
      if (table.Rows.Count > 0)
      {
        this.comboBox3.SelectedIndex = 0;
      }
      //初始化学生
      string sql_sname = "select name from student";
      table.Clear();
      table = SqlManage.TableSelect(sql_sname);
      string t_sname;
      foreach (DataRow row in table.Rows)
      {
        t_sname = row["name"].ToString();
        this.comboBox4.Items.Add(t_sname);
      }
      if (table.Rows.Count > 0)
      {
        this.comboBox4.SelectedIndex = 0;
      }
    }
    private void button2_Click(object sender, EventArgs e)
    {
      this.Close();
    }
    private void button1_Click(object sender, EventArgs e)
    {
      string sql = "";
      if (this.radioButton1.Checked)
      {
        sql = string.Format("select t_id as 教师编号,t_name as 教师姓名,t_age as 年龄 from teacher where t_id = '{0}'",
          this.comboBox1.Text);
      }
      else if (this.radioButton2.Checked)
      {
        sql = string.Format("select t_id as 教师编号,t_name as 教师姓名,t_age as 年龄 from teacher where t_name = '{0}'",
          this.comboBox2.Text);
      }
      else if (this.radioButton3.Checked)
      {
        sql = string.Format("select id as 学生编号,name as 学生姓名,sage as 年龄 from student where id = '{0}'",
          this.comboBox3.Text);
      }
      else if (this.radioButton4.Checked)
      {
        sql = string.Format("select id as 学生编号,name as 学生姓名,sage as 年龄 from student where name = '{0}'",
          this.comboBox4.Text);
      }
      DataTable table = SqlManage.TableSelect(sql);
      if (table.Rows.Count > 0)
      {
        this.dataGridView1.DataSource = table;
      }
      else
      {
        MessageBox.Show("没有相关内容");
      }
    }
    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
      if (this.radioButton1.Checked)
      {
        this.comboBox1.Enabled = true;
      }
      else
      {
        this.comboBox1.Enabled = false;
      }
    }
    private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
      if (this.radioButton2.Checked)
      {
        this.comboBox2.Enabled = true;
      }
      else
      {
        this.comboBox2.Enabled = false;
      }
    }
    private void radioButton3_CheckedChanged(object sender, EventArgs e)
    {
      if (this.radioButton3.Checked)
      {
        this.comboBox3.Enabled = true;
      }
      else
      {
        this.comboBox3.Enabled = false;
      }
    }
    private void radioButton4_CheckedChanged(object sender, EventArgs e)
    {
      if (this.radioButton4.Checked)
      {
        this.comboBox4.Enabled = true;
      }
      else
      {
        this.comboBox4.Enabled = false;
      }
    }
  }
} 

以上所述是小编给大家介绍的c#操作sql server2008 的界面实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

(0)

相关推荐

  • C#控制台程序实现开启、关闭SQLServer服务的代码分享

    用了近一天的时间研究了C#如何开启SqlServer数据库的服务,起先应用的是C#自带的System.ServiceProcess.ServiceContorller类,但个人认为它在win7下效果不佳,或许由于个人系统问题,最终决定放弃去选择应用C#的system.diagnostice.process.start方法执行cmd指令,下面我示范开启SqlServer最具代表的两个服务mssqlserver与mssqlserveragent,下面是本人练习写的控制台下的代码,经由多次测试均可成功

  • C#访问SqlServer设置链接超时的方法

    本文实例讲述了C#访问SqlServer设置链接超时的方法.分享给大家供大家参考.具体实现方法如下: 下面这段代码设置超时时间为60秒,默认为30秒 using (connection) { SqlCommand sqlcommand = connection.CreateCommand(); sqlcommand.CommandTimeout = 60; //默认时间为 30 秒 sqlcommand.CommandText = sqlText; ... 希望本文所述对大家的C#程序设计有所帮

  • C#连接到sql server2008数据库的实例代码

    废话不多说了,直接给大家贴代码了,具体代码如下所示: namespace MyFirstApp { class Program { static void Main(string[] args) { SqlConnection conn = null; SqlCommand comm = null; SqlDataReader sdreader = null; try { string ConStr = "server=192.168.1.110;uid=sa;pwd=woaifr0828;da

  • SQL Server中调用C#类中的方法实例(使用.NET程序集)

    需求是这样的,我在.net程序里操作数据时将一些字段数据加密了,这些数据是很多系统共用的,其中一delphi程序也需要用到,并且需要将数据解密,由于我在.net里加密的方式比较特殊,在delphi程序里解密比较繁琐且要消耗很多时间,所以不得不让sqlserver调用程序集的方式来解决问题. 下面只是一个例子,贴出来共享. 建立一个dll,class,代码如下: 复制代码 代码如下: namespace MyDll {     public partial class MyClass     {

  • C#查询SqlServer数据库并返回单个值的方法

    本文实例讲述了C#查询SqlServer数据库并返回单个值的方法.分享给大家供大家参考.具体实现方法如下: static public string GetSqlAsString(string sqlText, SqlParameter[] sqlParameters, string databaseConnectionString) { string result = ""; SqlDataReader reader; SqlConnection connection = new S

  • C#如何实现对sql server数据库的增删改查

    一个专门实现sql server数据库的增删改查,以及将查询的结果返回成表格等功能,分享代码如下 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient;//第一步:引用与sql相关的命名空间 using System.Data;//引用表的命名空间 //行注释 ///段落注释 /// <summary> ///第二步: 把

  • C#更新SQLServer中TimeStamp字段(时间戳)的方法

    本文实例讲述了C#更新SQLServer中TimeStamp字段(时间戳)的方法.分享给大家供大家参考.具体实现方法如下: public partial class Form1 : Form { private SqlConnection mCnn = null; private long TimeStampValue; public Form1() { InitializeComponent(); mCnn = new SqlConnection(); mCnn.ConnectionStrin

  • C#实现异步连接Sql Server数据库的方法

    本文实例讲述了C#实现异步连接Sql Server数据库的方法.分享给大家供大家参考.具体分析如下: .net最新版提供了await方法,可以使我们可以很容易实现到数据库的异步连接 复制代码 代码如下: readonly string ConnectionString = "Data Source=database_server_name;Initial Catalog=Store;Integrated Security=True"; protected async void Exec

  • c#操作sql server2008 的界面实例代码

    先是查询整张表,用到combobox选择查询哪张表,最后用DataGridView显示 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsForms

  • Android实现客户端语音动弹界面实例代码

    今天为大家介绍一下语音动弹界面的实现,新版本的客户端大家应该都看过了,这里我就只简单的介绍一下控件布局了.你可以在这里看到本控件的完整源码:http://git.oschina.net/oschina/android-app/blob/master/osc-android-app/src/net/oschina/app/widget/RecordButton.java 首先,整体界面分三部分,最上层自定义ActionBar相信不需要我讲大家就能看出来了. 中间部分是文字动弹部分,主体就是一个设置

  • 使用Bootstrap框架制作查询页面的界面实例代码

    以Bootstrap框架来进行设计和开发,是目前国际上比较流行的一个趋势.很多软件公司在优化新产品时,因为其在js和控件上的综合优势,会选用这个开发框架. Bootstrap框架是一个前端UI设计的框架,它提供了统一的UI界面,简化了设计界面UI的过程(缺点是定制了界面,调整的余地不是太大).尤其是现在的响应时布局(我的理解是页面根据不同的分辨率,采用不同的页面元素的布局),在Bootstrap中很好的支持了,只要简单设置了属性,就能自动实现响应时布局,大大简化了程序员的界面的过程. 因此,本人

  • Boostrap实现的登录界面实例代码

    Bootstrap它是一个开源的web开发前端框架. 这几天我看了下Bootstrap的官方文档.看到其中的Basic-form,突然想实现下登录界面.然后想了下实现的思路,于是就打开了桌面的H5 builder码起来.代码实现起来其实不难,但是碰到个问题,就是Bootstrap的布局控制好像用.col类难以实现居中显示,虽然可以用modal(模态框)实现弹出居中,但是我暂时不想用modal框.发现问题后,第一想法是自己再定义个css进行一个控制.但是又不知道行业内的大牛是不是只用Bootstr

  • ViewPager 与 Fragment相结合实现微信界面实例代码

    在如今的互联网时代,微信已是一个超级App.这篇通过ViewPager + Fragment实现一个类似于微信的界面,之前有用FragmentTabHost实现过类似界面,ViewPager的实现方式相对于FragmentTabHost的方式更简单明了. ViewPager: ViewPager继承自ViewGroup,是一个容器类,可以往里添加View. ViewPager的使用很简单,通过setAdapter()方法设置一个PagerAdapter即可,这个PagerAdapter需要自己写

  • Android仿拉手网团购App我的收藏界面实例代码

    先给大家展示效果图,如果感觉还不错,请参考实例代码 效果图如下所示: 具体代码如下: private void initData() { BmobManager.getInstance(new BmobQueryCallback() { @Override public void onQuerySuccess(List<? extends BaseModel> dataList) { mDataList.clear(); List<FavorModel> list = (List&

  • Python操作使用MySQL数据库的实例代码

    Python 操作 MySQL 配置 win_64 Ubuntu14.04 Python3.x pip安装pymysql模块 直接使用pip安装 pip install pymysql win64上直接在cmd中执行 连接本地数据库 使用模块pymysql连接数据库 #!/usr/bin/python # coding=utf-8 import pymysql # 连接本地数据库 conn = pymysql.connect(host='localhost', port=3306, user='

  • Springboot整合MongoDB进行CRUD操作的两种方式(实例代码详解)

    1 简介 Springboot是最简单的使用Spring的方式,而MongoDB是最流行的NoSQL数据库.两者在分布式.微服务架构中使用率极高,本文将用实例介绍如何在Springboot中整合MongoDB的两种方法:MongoRepository和MongoTemplate. 代码结构如下: 2 项目准备 2.1 启动MongoDB实例 为了方便,使用Docker来启动MongoDB,详细指导文档请参考:基于Docker的MongoDB实现授权访问的方法,这里不再赘述. 2.2 引入相关依赖

  • 模仿百度红包福袋界面实例代码

    新年到新年到,红包抢不停.在我抢红包的时候意外的发现了百度的福袋界面挺不错的,于是抽时间专门写篇文章来完成百度红包界面吧. 当然啦,这其实就是解锁界面的进化版本.不过其包含的知识点还是挺多的,写篇博文记录一下看看具体有哪些技术点啦.看看百度的效果图: 1.编程思路 看看界面,不难发现,其就是一个放入九张图片的容器,绘制其实可以在其上面另创建一个透明View负责绘制线与圆圈.下面我们将介绍一下实现过程. ㈠自定义ViewGroup 我们知道,自定义ViewGroup一定需要实现其onLayout(

随机推荐