Get the App
SLTechnology News&Howtos  ›  Database  › 

C # connects to SQL Server,SQLite database

Shulou Source: shulou.com Published: 2022-06-01 11:19:08 10月02日 Update

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 ()

Tags: Test data temperature database caching code file date time location instance datasheet humidity statement link Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Technology Apple Microsoft Docker