C#中连接SQL Server的代码:string con, sql; con = "Server=.;Database=温湿度测试;Trus
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();
--结束END--
本文标题: C#连接到SQL Server,SQLite数据库
本文链接: https://www.lsjlt.com/news/42216.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-08
2024-05-08
2024-05-08
2024-05-08
2024-05-08
2024-05-08
2024-05-08
2024-05-08
2024-05-08
2024-05-08
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0