日期:2009-06-09 浏览次数:20465 次
string strCon = "Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial Catalog = data1 ; Data Source = server1 " ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; myConn.Open ( ) ; |
using System ; using System.Drawing ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data.OleDb ; using System.Data ; //导入程序中使用到的名称空间 public class DataAdd : Form { private Button lastrec ; private Button nextrec ; private Button previousrec ; private Button firstrec ; private Container components ; private Label title ; private Button t_new ; private Button save ; private TextBox t_bookstock ; private TextBox t_bookprice ; private TextBox t_bookauthor ; private TextBox t_booktitle ; private TextBox t_bookid ; private Label l_bookstock ; private Label l_bookprice ; private Label l_bookauthor ; private Label l_booktitle ; private Label l_bookid ; private DataSet myDataSet ; private BindingManagerBase myBind ; //定义在程序中要使用的组件 public DataAdd ( ) { //连接到一个数据库 GetConnected ( ) ; // 对窗体中所需要的内容进行初始化 InitializeComponent ( ); } //释放程序使用过的所以资源 public override void Dispose ( ) { base.Dispose ( ) ; components.Dispose ( ) ; } public static void Main ( ) { Application.Run ( new DataAdd ( ) ) ; } public void GetConnected ( ) { try{ // 设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称为server1,数据库为data1,用户名为sa。 string strCon = "Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial Catalog = data1 ; Data Source = server1 " ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; myConn.Open ( ) ; string strCom = " SELECT * FROM books " ; //创建一个 DataSet myDataSet = new DataSet ( ) ; //用 OleDbDataAdapter 得到一个数据集 OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ; //把Dataset绑定books数据表 myCommand.Fill ( myDataSet , "books" ) ; //关闭此OleDbConnection myConn.Close ( ) ; } catch ( Exception e ) { MessageBox.Show ( "连接错误! " + e.ToString ( ) , "错误" ) ; } } private void InitializeComponent ( ) { components = new System.ComponentModel.Container ( ) ; nextrec = new Button ( ) ; lastrec = new Button ( ) ; previousrec = new Button ( ) ; firstrec = new Button ( ) ; t_bookprice = new TextBox ( ) ; l_booktitle = new Label ( ) ; l_bookprice = new Label ( ) ; l_bookauthor = new Label ( ) ; t_bookid = new TextBox ( ) ; save = new Button ( ) ; title = new Label ( ) ; t_bookauthor = new TextBox ( ) ; t_booktitle = new TextBox ( ) ; t_new = new Button ( ) ; l_bookstock = new Label ( ) ; t_bookstock = new TextBox ( ) ; l_bookid = new Label ( ) ; //以下是对数据浏览的四个按钮进行初始化 firstrec.Location = new System.Drawing.Point ( 65 , 312 ) ; firstrec.ForeColor = System.Drawing.Color.Black ; firstrec.Size = new System.Drawing.Size ( 40 , 24 ) ; firstrec.Font = new System.Drawing.Font("仿宋", 8f ); firstrec.Text = "首记录"; firstrec.Click += new System.EventHandler(GoFi
免责声明: 本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
|