C#连接到SQLServer,SQLite数据库
C#中连接SQL Server的代码:

创新互联是一家网站设计公司,集创意、互联网应用、软件技术为一体的创意网站建设服务商,主营产品:响应式网站、高端网站设计、全网营销推广。我们专注企业品牌在网站中的整体树立,网络互动的体验,以及在手机等移动端的优质呈现。成都网站建设、网站建设、移动互联产品、网络运营、VI设计、云产品.运维为核心业务。为用户提供一站式解决方案,我们深知市场的竞争激烈,认真对待每位客户,为客户提供赏析悦目的作品,网站的价值服务。
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();
新闻标题:C#连接到SQLServer,SQLite数据库
转载注明:http://www.cqwzjz.cn/article/jhhiss.html


咨询
建站咨询
