(C#)链接本地数据SDF。

1、添加命名空间


using System.Data.SqlServerCe;
using System.Configuration;



2、添加引用:   System.Configuration



查询语句:

SqlCeConnection sqlceCon = new SqlCeConnection();
 SqlCeCommand sqlCeCom = new SqlCeCommand();
 string P_Str_ConnectionStr = System.Configuration.ConfigurationManager.ConnectionStrings["dbTest.Properties.Settings.LocalDBConnectionString"].ConnectionString;
 DataTable dt = new DataTable();
 sqlceCon = new SqlCeConnection(P_Str_ConnectionStr);
 sqlceCon.Open();
 SqlCeDataAdapter adptrOdbc = new SqlCeDataAdapter("select * from Test", sqlceCon);
 adptrOdbc.Fill(dt);
 dataGridView1.DataSource = dt;
 sqlceCon.Close();


插入语句:


private int doSQL(string sqlString)
        {
            sqlceCon = new SqlCeConnection(P_Str_ConnectionStr);
            sqlceCon.Open();
            SqlCeCommand cmd = new SqlCeCommand(sqlString, sqlceCon);
            int count=cmd.ExecuteNonQuery();
            sqlceCon.Close();
            return count;
        }
            string Id = textBox1.Text;
            string Name = textBox2.Text;
            string Number = textBox3.Text;
            string SqlString = "insert into TEST(ID,Name,Number)values('" + Id + "','" + Name + "','" + Number + "')";

             int count=doSQL(SqlString);




删除语句:


string SqlString = "delete Test where Name='"+Name+"'";
 int count=doSQL(SqlString);






     

你可能感兴趣的:((C#)链接本地数据SDF。)