使用sql的一些笔记

sql:

C#写入sql命令:
               string str = "server=.;database=People;Integrated Security=SSPI;";


                SqlConnection con = new SqlConnection(str); //创建连接对象
                con.Open(); //打开连接
                string strsql = "insert into pe(id,name,age,birthday) values('3','sanchuan','22','201212')"; // 编写SQL语句
                SqlCommand sqlcommand = con.CreateCommand();
                sqlcommand.CommandText = strsql;
                
                sqlcommand.ExecuteNonQuery();
                //SqlCommand cmd = new SqlCommand(strsql, con);   
                con.Close();
C#读取TX然后写进数据库中:
 DataTable table = new DataTable();
            string[] ColumnsName={
                                     "Timestamp",
                                     "S1",
                                     "S2",
                                     "S3",
                                     "S4",
                                     "S5",
                                     "S6",
                                     "S7",
                                     "S8",
                                     "S9",
                                     "S10",
                                     "T1","T2","T3","T4","T5","T6","T7","T8","T9","T10"
                                 };
            for (int i = 0; i < ColumnsName.Length; i++)
            {
                table.Columns.Add(ColumnsName[i], typeof(SqlString));
            }
            //table.Columns.Add("Timestamp", typeof(SqlString));




            string text = File.ReadAllText(@"E:\program\测评\Sensors.20160612182433.txt");
            string[] str1 = text.Split('\n');
            int str1_length = str1.Length;
            for (int i = 3; i < str1_length-2; i++)
            {
                string[] str1_child = str1[i].Split('\t');
                int a=str1_child.Length;
                DataRow row = table.NewRow();
                for (int j = 0; j < ColumnsName.Length; j++)
                {
                    String ac = ColumnsName[j];
                    row[ac]=str1_child[j];
                }
                table.Rows.Add(row);






            }
            dataGridView1.DataSource = table;
            string str = "server=.;database=PlatformFlawBase;Integrated Security=SSPI;";
            SqlConnection con = new SqlConnection(str); //创建连接对象
            con.Open(); //打开连接
            SqlBulkCopy bulkcopy = new SqlBulkCopy(con);
            bulkcopy.DestinationTableName = "dbo.SanChuan";
            bulkcopy.WriteToServer(table);

从数据库中取出并且使用dataset来处理
           string str = "server=.;database=People;Integrated Security=SSPI;";
            SqlConnection conn = new SqlConnection(str);
            string sqlqury = "select ID from Person;";
            conn.Open();
            SqlDataAdapter sdr = new SqlDataAdapter(sqlqury,conn);
            DataSet ds = new DataSet();   //dataset相当于临时的数据库,里面可以存放很多的table,
            sdr.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0].DefaultView;    //默认存放于table【0】中
            DataRow da = ds.Tables[0].Rows[0];          //可以按照行row[0],列colum[0],第二列第一行colum[2][1]来取,所取得为一个datarow数组,然后再取此数组中的某一行
            button1.Text = Convert.ToString(da[0]);     //显示类型转换,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值