您好,登錄后才能下訂單哦!
C#中連接SQL Server的代碼:
string con, sql; con = "Server=.;Database=溫濕度測試;Trusted_Connection=SSPI"; sql = "select * from 溫濕度測試"; SqlConnection mycon = new SqlConnection(con); DateTime uploadDate = System.DateTime.Now; mycon.Open(); SqlDataAdapter myda = new SqlDataAdapter(sql, con); DataSet myds = new DataSet(); myda.Fill(myds, "溫濕度測試"); String sql2 = "insert into 溫濕度測試(日期,時間,溫度,濕度) values('" + uploadDate + "','" + uploadDate + "','" + wendu + "','" + shidu + "')"; SqlDataAdapter myda2 = new SqlDataAdapter(sql2, mycon); myda2.Fill(myds, "溫濕度測試"); dataGridView1.DataSource = myds.Tables["溫濕度測試"]; mycon.Close();
下面是C#鏈接SQLite的代碼:
//sqlite 數據庫 string uploadDate = System.DateTime.Now.ToShortDateString().ToString(); string uploadTime = System.DateTime.Now.ToLongTimeString().ToString(); SQLiteConnection conn = null; string dbPath = "Data Source =" + "C:/sqlite" + "/數據測試.db"; conn = new SQLiteConnection(dbPath);//創建數據庫實例,指定文件位置 conn.Open();//打開數據庫,若文件不存在會自動創建 string sql = "CREATE TABLE IF NOT EXISTS '" + "溫度" + uploadDate + "' (日期 varchar(6), 時間 varchar(6), 溫度 varchar(6));";//建表語句 SQLiteCommand cmdCreateTable = new SQLiteCommand(sql, conn); cmdCreateTable.ExecuteNonQuery();//如果表不存在,創建數據表 SQLiteCommand cmdInsert = new SQLiteCommand(conn); cmdInsert.CommandText = "INSERT INTO '" + "溫度" + uploadDate + "' VALUES('" + uploadDate + "','" + uploadTime + "','" + wendu + "')";//插入幾條數據 cmdInsert.ExecuteNonQuery(); conn.Close();
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。