C # connects to SQL Server,SQLite database
The code to connect to SQL Server in C #:
String con, sql; con = "Server=.;Database= temperature and humidity test; Trusted_Connection=SSPI"; sql = "select * from temperature and humidity test"; 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, "temperature and humidity test") String sql2 = "insert into temperature and humidity Test (date, time, temperature, humidity) values ('" + uploadDate + "','" + uploadDate + "','" + wendu + ",'" + shidu + "')"; SqlDataAdapter myda2 = new SqlDataAdapter (sql2, mycon); myda2.Fill (myds, "temperature and humidity Test"); dataGridView1.DataSource = myds.Tables ["temperature and humidity Test"]; mycon.Close ()
Here is the code for the C# link SQLite:
/ / sqlite database string uploadDate = System.DateTime.Now.ToShortDateString () .ToString (); string uploadTime = System.DateTime.Now.ToLongTimeString () .ToString (); SQLiteConnection conn = null; string dbPath = "Data Source =" + "C:/sqlite" + "/ data test .db"; conn = new SQLiteConnection (dbPath); / / create a database instance and specify the file location conn.Open () / / Open the database, and automatically create string sql = "CREATE TABLE IF NOT EXISTS'" + "temperature" + uploadDate + "'(date varchar (6), time varchar (6), temperature varchar (6)) if the file does not exist; / / create the table statement SQLiteCommand cmdCreateTable = new SQLiteCommand (sql, conn); cmdCreateTable.ExecuteNonQuery (); / / if the table does not exist, create the data table SQLiteCommand cmdInsert = new SQLiteCommand (conn) CmdInsert.CommandText = "INSERT INTO'" + "temperature" + uploadDate + "'VALUES ('" + uploadDate + "','" + uploadTime + "','" + wendu + "')"; / / insert several pieces of data cmdInsert.ExecuteNonQuery (); conn.Close ()