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;
using System.Data.SqlClient;
using System.Data.Common;
namespace 双色球随机选号器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DataSet ds1 = new DataSet();
string consqlserver = "Data Source = .;Initial Catalog = CPSJ;User Id = sa;Password = sa;"; //定义连接数据库字符串
string sql = "select top 6 * from TBLLottery where type='红' order by newid()";//定义SQL查询字符串
string sql1 = "select top 1 * from TBLLottery where type='蓝' order by newid()";
SqlConnection con = new SqlConnection(consqlserver);//实例化con
SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.Fill(ds1);//填充数据
DataTable dt = new DataTable();
dt = ds1.Tables[0];
textBox1.Text = Convert.ToString(dt.Rows[0]["number"]);//获取数据源赋值数据库控件
textBox3.Text = Convert.ToString(dt.Rows[1]["number"]);//获取数据源赋值数据库控件
textBox4.Text = Convert.ToString(dt.Rows[2]["number"]);//获取数据源赋值数据库控件
textBox5.Text = Convert.ToString(dt.Rows[3]["number"]);//获取数据源赋值数据库控件
textBox6.Text = Convert.ToString(dt.Rows[4]["number"]);//获取数据源赋值数据库控件
textBox7.Text = Convert.ToString(dt.Rows[5]["number"]);//获取数据源赋值数据库控件
/*蓝球输出*/
DataSet dse = new DataSet();
SqlDataAdapter da1 = new SqlDataAdapter(sql1, con);
da.Fill(dse);//填充数据
DataTable dt1 = new DataTable();
dt1 = ds1.Tables[0];
textBox2.Text = Convert.ToString(dt1.Rows[0]["number"]);
con.Close(); //关闭连接
con.Dispose();//释放连接
da.Dispose(); //释放资源
}
}
}
下面是:按钮布局图
数据库请参照:我微博内的SQL的NEWID用法