【WPF学习笔记】之如何设置下拉框读取SqlServer数据库的值:动画系列之(一)
先前条件:设置好数据库,需要三个文件CommandInfo.cs、DbHelperSQL.cs、myHelper.cs,需要修改命名空间,参照之前随笔http://www.cnblogs.com/Owen-ET/p/5999654.html
添加一个表的类User_test对应数据库的表,如下图:
User_test类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data; namespace User.sqlHelper
{
[Serializable]
class User_test
{
public User_test()
{ } #region
private int _userID;
private string _userName;
private int _userGrade;
private string _userPassword; public int UserID
{
set { _userID = value; }
get { return _userID; }
} public string UserName
{
set { _userName = value;}
get { return _userName; }
} public int UserGrade
{
set { _userGrade = value; }
get { return _userGrade; }
} public string UserPassword
{
set { _userPassword = value;}
get { return _userPassword; }
} #endregion #region public User_test(int userID)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select UserID,UserName,UserGrade");
strSql.Append(" FROM [User] ");
strSql.Append(" where UserID=@userID"); SqlParameter[] parameter = {
new SqlParameter("@userID",SqlDbType.Int,)};
parameter[].Value = userID; DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameter);
if( ds.Tables[].Rows.Count > )
{
if (ds.Tables[].Rows[]["UserID"] != null) { this.UserID = int.Parse(ds.Tables[].Rows[]["UserID"].ToString().Trim()); }
if (ds.Tables[].Rows[]["UserName"] != null) { this.UserName = ds.Tables[].Rows[]["UserName"].ToString().Trim(); }
if (ds.Tables[].Rows[]["UserGrade"] != null) { this.UserGrade = int.Parse(ds.Tables[].Rows[]["UserGrade"].ToString().Trim()); }
if (ds.Tables[].Rows[]["UserPassword"] != null) { this.UserPassword = ds.Tables[].Rows[]["UserPassword"].ToString().Trim(); }
}
} //根据id得到名字
public string getUserName(int userid)
{
string name = "";
StringBuilder strSql = new StringBuilder();
strSql.Append("select UserName ");
strSql.Append(" FROM [User] ");
strSql.Append("where UserID=@userID");
SqlParameter[] parameter = {
new SqlParameter("@userID",SqlDbType.Int,)};
parameter[].Value = userid;
DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameter); if(ds.Tables[].Rows.Count > )
{
if (ds.Tables[].Rows[]["UserName"] != null) { name = ds.Tables[].Rows[]["UserName"].ToString().Trim(); }
}
return name;
} //根据名字得到级别
public int getUserGrade(string username)
{
int grade = ;
StringBuilder strSql = new StringBuilder();
strSql.Append("select UserGrade ");
strSql.Append(" FROM [User] ");
strSql.Append(" where UserName=@userName");
SqlParameter[] parameter = {
new SqlParameter("@userName", SqlDbType.VarChar,)};
parameter[].Value = username;
DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameter); if(ds.Tables[].Rows.Count > )
{
if (ds.Tables[].Rows[]["UserGrade"] != null) { grade = int.Parse(ds.Tables[].Rows[]["UserGrade"].ToString().Trim()); }
}
return grade;
} /// <summary>
/// 增加一条数据
/// </summary>
///
public int Add(string username)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into [User] (");
strSql.Append("UserName)");
strSql.Append(" values(");
strSql.Append("@userName)");
SqlParameter[] parameter = {
new SqlParameter("@userName",SqlDbType.VarChar,)};
parameter[].Value = username; object obj = DbHelperSQL.Query(strSql.ToString(), parameter);
if (obj == null)
{
return ;
}
else
{
return Convert.ToInt32(obj);
}
} /// <summary>
/// 删除一条数据
/// </summary>
public bool Delete(int userid)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("delete from [User] ");
strSql.Append(" where UserID=" + UserID.ToString()); int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
if (rows > )
{
return true;
}
else
{
return false;
}
} /// <summary>
/// 获得数据列表
/// </summary>
///
public DataSet GetList()
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select * ");
strSql.Append("FROM [User]");
return DbHelperSQL.Query(strSql.ToString());
} #endregion
}
}
对应数据库:

然后在xaml中设置下拉框:

<UserControl x:Class="User.uc_login"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Loaded="UserControl_Loaded"
d:DesignHeight="1080" d:DesignWidth="1920">
<Canvas x:Name="c_login" Width="1920" Height="1080" Background="Azure">
<Canvas x:Name="c_log" Width="600" Height="600" Canvas.Left="680" Canvas.Top="100" Background="AntiqueWhite">
<Label Content="用户:" Width="122" Height="80" FontSize="35" Canvas.Left="100" Canvas.Top="105" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
<ComboBox x:Name="cb_uploader" Width="200" Height="80" Canvas.Left="268" Canvas.Top="105" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="35"/>
<Label Content="密码:" Width="122" Height="80" FontSize="35" Canvas.Left="100" Canvas.Top="230" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
<TextBox x:Name="tb_password" Width="200" Height="80" Canvas.Left="268" Canvas.Top="230" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="35"/>
<Button Content="登录" Width="120" Height="60" FontSize="32" Canvas.Left="100" Canvas.Top="360" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Click="btn_login_Click"/>
<!--<Button Content="修改密码" Width="165" Height="60" FontSize="32" Canvas.Left="305" Canvas.Top="460" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" />-->
</Canvas>
</Canvas>
</UserControl>
注:上面代码xaml是用户控件代码。

最后,在后台代码设置:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using User.sqlHelper;
using System.Data; namespace User
{
/// <summary>
/// uc_login.xaml 的交互逻辑
/// </summary>
public partial class uc_login : UserControl
{
public uc_login()
{
InitializeComponent();
} private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
//获取下拉框用户名字
User_test _u = new User_test();
DataSet _ds = _u.GetList();
if(_ds != null)
{
DataTable _dt = _ds.Tables[];
for (int i = ; i < _dt.Rows.Count; i++)
{
string UserName = _dt.Rows[i]["UserName"].ToString().Trim();
ComboBoxItem cbitem = new ComboBoxItem();
cb_uploader.Items.Add(cbitem);
cbitem.Content = UserName;
}
}
} //登录按钮
private void btn_login_Click(object sender, RoutedEventArgs e)
{
//判断下拉框不为空
if (this.cb_uploader != null)
{
//判断密码不为空
if (this.tb_password != null)
{
string uploader = cb_uploader.Items.
}
//密码为空
else
{ }
}
//下拉框为空
else
{ }
}
}
}
结果:

【WPF学习笔记】之如何设置下拉框读取SqlServer数据库的值:动画系列之(一)的更多相关文章
- JS学习笔记 - fgm练习 - 输入法下拉框 三元表达式
<script> window.onload = function() { var oBtn = document.getElementsByTagName('input')[0]; va ...
- Jquery动态设置下拉框selected --(2018 08/12-08/26周总结)
1.Jquery动态根据内容设置下拉框selected 需求就是根据下拉框的值动态的设置为selected,本以为很简单,网上一大推的方法,挨着尝试了之后却发现没有一个是有用的.网上的做法如下: &l ...
- js 设置下拉框的默认值
设置下拉框的默认值,直接在option中增加selected就可以了.但是现在要使用JS来设置它的默认值,代码如下: <select name="aaa" id=" ...
- jquery 根据后台传过来的值动态设置下拉框、单选框选中
更多内容推荐微信公众号,欢迎关注: jquery 根据后台传过来的值动态设置下拉框.单选框选中 $(function(){ var sex=$("#sex").val(); va ...
- java操作poi生成excel.xlsx(设置下拉框)下载本地和前端下载
需求:导入excel表格,如果excel有错误,将错误的地方标红,在把数据以excel的形式写出,供用户下载解决方案:1.以实体类的方式接收excel并解析(创建两个集合一个接收正常的数据一个接收错误 ...
- jquery设置下拉框selected不起作用
在js中设置下拉框被选中: 最初写法: //移出selected $("#selected option").removeAttr("selected"); / ...
- form表单传递下拉框的Value和Text值,不适用Jquery传递
同时获取下拉框的Value和Text值的解决办法:添加一个<input type="text" >文本框,用户选中一项后就将该项的value值给他 然后接受页面获取该文 ...
- JavaScript获取select下拉框中的第一个值
JavaScript获取select下拉框中的第一个值 1.说明 获取select下拉框中的第一个值 2.实现源码 <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- jq select change下拉框选项变化判断选中值,添加(attr)或移除(removeAttr)一个或多个属性
select change下拉框选项变化判断选中值,添加(attr)或移除(removeAttr)一个或多个属性 $("#IsRecommend").change(function ...
随机推荐
- C语言字符串操作总结大全(超详细)【转】
转自:http://www.jb51.net/article/37410.htm )字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strc ...
- [Oracle] 临时将Physical Standby激活
Oracle 10g/11g下如何将物理Standby库临时激活用于测试 在实际运营环境中, 我们经常碰到类似这样的需求: 譬如想不影响现网业务评估DB补丁在现网环境中运行的时间, 或者是想在做DB切 ...
- 有道词典中的OCR功能:第三方库的变化
之前有点好奇有道词典中的OCR功能,具体来说就是强力取词功能.我知道的最有名的OCR库是tesseract,这个库是惠普在早些年前开源的. 在用python做爬虫处理验证码的时候,就会用到这个库,对应 ...
- DB2数据库报 [SQL0805N Package "NULLID.SQLLD003" was not found.]
解决办法: cd /home/db2inst1/sqllib/bnddb2 bind @db2cli.lst blocking all grant public sqlerror continue C ...
- Android布局实现阴影效果
最近某个模块的UI,设计想要卡片式阴影效果.之前查阅过资料,用传统的xml方式作为布局的background <?xml version="1.0" encoding=&qu ...
- HDU 5242 Game(树上贪心)
题目链接 Game 题目的意思很简单, 就是要找一棵树权值最大等等前K条链. 在本题中,走的次数等于min(叶子结点个数,k) tree[i].sum意为从i号结点出发走到某个叶子结点能得到的最大总价 ...
- iOS 动画笔记 (一)
你也肯定喜欢炫酷的动画! 在APP中,动画就是一个点睛之笔!可以给用户增加一些独特的体验感,估计也有许多的和我一样的,看着那些觉得不错的动画,也就只能流口水的孩子,毕竟可能不知道从哪里下手去写!动画学 ...
- 洛谷——P1306 斐波那契公约数
P1306 斐波那契公约数 题目描述 对于Fibonacci数列:1,1,2,3,5,8,13......大家应该很熟悉吧~~~但是现在有一个很“简单”问题:第n项和第m项的最大公约数是多少? 输入输 ...
- PHP如何在页面中原样输出HTML代码
字符串与HTML之间的相互转换主要应用htmlentities()函数来完成. header("Content-Type: text/html; charset=utf-8"); ...
- 第十八章 Python批量管理主机(paramiko、fabric与pexpect)
这个人的文章不错:http://lizhenliang.blog.51cto.com/all/7876557 转载:http://lizhenliang.blog.51cto.com/7876557/ ...