[英]Retrieve data from sql server compact 4.0 to textbox
I have two text boxes in my winform. 我的Winform中有两个文本框。 I would like to enter a userId into first text box, after that by clicking a button display a User Name in the second text box properly.
我想在第一个文本框中输入一个userId,然后单击一个按钮在第二个文本框中正确显示一个用户名。 The data is stored in sql server compact.
数据存储在sql server compact中。 Table name is
Users
, and this table contains two columns UserID
and UserName
. 表名是
Users
,该表包含两列UserID
和UserName
。
With this code I can open a connection and retrieve the first value from the UserName column, 使用此代码,我可以打开一个连接并从UserName列中检索第一个值,
SqlCeConnection cn = new SqlCeConnection(@"Data Source = D:\Database\Training.sdf");
try
{
cn.Open();
SqlCeCommand cmd = new SqlCeCommand("SELECT UserID, UserName from Users;", cn);
TrainerNameBox.Text = cmd.ExecuteScalar().ToString();
cn.Close();
}
catch
{
}
ExecuteScalar
returns first column of the first row. ExecuteScalar
返回第一行的第一列 。 Other columns or rows are ignored. 其他列或行将被忽略。
In your case, your first column is UserID
. 在您的情况下,第一列是
UserID
。 That's why you get first value of this column. 这就是为什么您获得此列的第一个值的原因。
If you want to get UserName
value, you might need to change your query like; 如果要获取
UserName
值,则可能需要更改查询,例如:
SELECT UserName from Users
And looks like you forget to use WHERE
clause in your query since you want to get UserName
from UserID
. 而且看起来您忘记了在查询中使用
WHERE
子句,因为您想从UserID
获取UserName
。 You might need to use using
statement to dispose your SqlCeConnection
and SqlCeCommand
. 您可能需要使用
using
语句来处理SqlCeConnection
和SqlCeCommand
。
Full example; 完整的例子;
using(SqlCeConnection cn = new SqlCeConnection(@"Data Source = D:\Database\Training.sdf"))
using(SqlCeCommand cmd = cn.CreateCommand())
{
cmd.CommandText = "SELECT UserName from Users WHERE UserID = @id";
cmd.Parameters.AddWithValue("@id", (int)txtUserID.Text);
cn.Open();
TrainerNameBox.Text = cmd.ExecuteScalar().ToString();
}
You are missing the WHERE clause to isolate the username that you want to display 您缺少WHERE子句以隔离要显示的用户名
int userID;
if(!Int32.TryParse(txtUserID.Text, out userID))
{
MessageBox.Show("Invalid User ID number");
return;
}
using(SqlCeConnection cn = new SqlCeConnection(@"Data Source = D:\Database\Training.sdf"))
using(SqlCeCommand cmd = new SqlCeCommand("SELECT UserName from Users WHERE UserID=@id;", cn))
{
cn.Open();
cmd.Parameters.AddWithValue("@id", userID);
object result = cmd.ExecuteScalar();
if(result != null)
TrainerNameBox.Text = result.ToString();
else
MessageBox.Show("No user for ID=" + userID.ToString());
}
Notice that ExecuteScalar
returns the first column of the first row, so you need to remove the UserID field from your query and if, the user is not found, you need to check for a null return. 注意,
ExecuteScalar
返回第一行的第一列,因此您需要从查询中删除UserID字段,如果找不到该用户,则需要检查是否为空。
Applying directly the ToString() method to your ExecuteScalar could raise an exception if your user types an invalid id. 如果用户键入无效的ID,则直接将ToString()方法应用于ExecuteScalar可能会引发异常。 There is also the problem to validate the user input.
还有验证用户输入的问题。 If you type a not numeric value for the user id, the conversion will fail.
如果您为用户标识键入一个非数字值,则转换将失败。 In this case you need to check the input using Int32.TryParse
在这种情况下,您需要使用Int32.TryParse检查输入
Try this: 尝试这个:
Dataset ds = cmd.ExecuteDataset().ToString();
TrainerNameBox.Text = ds.tables[0].Rows[0][1].toString();
TrainerIDBox.Text = ds.tables[0].Rows[0][0].toString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.