从本地数据库获取数据(.sdf) [英] get data form local database(.sdf)

查看:242
本文介绍了从本地数据库获取数据(.sdf)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C#Windows表单应用程序

我的程序将数据存储在本地数据库(.sdf)中。我不能从数据库中获取数据

没有错误没有从数据库中检索数据
这个我的代码这是我的第一个本地数据库程序

有什么不对?

有没有其他方法可以做到这一点?



表名= tbl_user

列名= u_name



 SqlCeConnection con =  new  SqlCeConnection( 数据源= H :\\application\\database.sdf); 
尝试
{
SqlCeCommand com = new SqlCeCommand( SELECT * FROM tbl_user WHERE u_id = @ u_id,con);
com.Parameters.AddWithValue( u_id 1 );
SqlCeDataReader reader = com.ExecuteReader();
string name = reader [ u_name ]的ToString();
MessageBox.Show(name);
con.Close();
}
catch (SqlCeException)
{
MessageBox.Show( 错误);

}







提前感谢!

解决方案

首先应该更换

 SqlCeConnection con =  new  SqlCeConnection(  Data Source = H:\ application\database.sdf  ); 



with

 SqlCeConnection con =  new  SqlCeConnection( 数据源=   H:\\application \\database。 sdf  ); 



并查看它是否有所改变。



字符串中的引号必须加倍或转义;反斜杠也一样。



你可以在这里找到参考资料:

字符串文字 [ ^ ]



[edit]



你似乎没有打开你的联系;这也可能是问题。

更好的处理方法当然是:

 使用(SqlCeConnection con =  new  SqlCeConnection(  Data Source =   H:\\application\\database.sdf   )){
尝试 {
con.Open();
// ...其余代码......
}
catch (SqlCeException e){
MessageBox.Show(e.Message); // 这将是一个改进,而不仅仅是'错误'这个词
}
}





[/ edit]


它看起来读者没有获取数据
你应该这样做

string apppath = Application.StartupPath.ToString()+\\Northwind.sdf;

SqlConnection con = new SqlConnection(Data Source = KTPL11 \\SQLEXP_KTPL11; Initial Catalog = Trial; Integrated Security = true);



试试

{

con.Open();

SqlCommand com = new SqlCommand(SELECT * FROM tbl_user WHERE u_id = 1,con);

com.CommandType = CommandType.Text;

com.Parameters.AddWithValue(u_id ,1);

使用(SqlDataReader reader = com.ExecuteReader())

{

string name =;

if(reader.Read())

{

name = reader [u_name]。ToString();

}

MessageBox.Show(name);

}

con.Close();

}

catch(SqlException ex)

{

MessageBox.Show(Error);



}

I'm using a C# Windows form application
My program store data in local database (.sdf). I can't get data from the database.
No errors but no retrieving data from database
this my code this is my first local database program
what is the wrong ?
Is there any other way to do this ?

table name = tbl_user
column name = u_name

SqlCeConnection con = new SqlCeConnection("Data Source=H:\\application\\database.sdf");
            try
            {
                SqlCeCommand com = new SqlCeCommand("SELECT * FROM tbl_user WHERE u_id=@u_id", con);
                com.Parameters.AddWithValue("u_id", 1);
                SqlCeDataReader reader = com.ExecuteReader();
                string name = reader["u_name"].ToString();
                MessageBox.Show(name);
                con.Close();
            }
            catch (SqlCeException)
            {
                MessageBox.Show("Error");
               
            }




Thank in Advance!

解决方案

You should start by replacing

SqlCeConnection con = new SqlCeConnection("Data Source="H:\application\database.sdf"");


with

SqlCeConnection con = new SqlCeConnection("Data Source=""H:\\application\\database.sdf""");


and see if it changes something.

Quotes in a string have to be doubled or escaped ; and it is the same with backslashes.

You can find the reference here:
String literals[^]

[edit]

You do not seem to ever open your connection ; that can be the problem, also.
A better way to handle that would certainly be:

using (SqlCeConnection con = new SqlCeConnection("Data Source=""H:\\application\\database.sdf""")) {
   try {
      con.Open();
      // ... rest of the code ...
   }
   catch (SqlCeException e) {
      MessageBox.Show(e.Message); // Would be an improvement than just the word 'Error'
   }
}



[/edit]


it seeems that reader is not fetching data
you should do like
string apppath = Application.StartupPath.ToString() + "\\Northwind.sdf";
SqlConnection con = new SqlConnection("Data Source=KTPL11\\SQLEXP_KTPL11;Initial Catalog=Trial;Integrated Security=true");

try
{
con.Open();
SqlCommand com = new SqlCommand("SELECT * FROM tbl_user WHERE u_id=1", con);
com.CommandType = CommandType.Text;
com.Parameters.AddWithValue("u_id", 1);
using (SqlDataReader reader = com.ExecuteReader())
{
string name = "";
if (reader.Read())
{
name = reader["u_name"].ToString();
}
MessageBox.Show(name);
}
con.Close();
}
catch (SqlException ex)
{
MessageBox.Show("Error");

}


这篇关于从本地数据库获取数据(.sdf)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆