码迷,mamicode.com
首页 > 其他好文 > 详细

qwer

时间:2017-10-26 20:01:32      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:empty   value   控件   inpu   exce   文本框   int   use   readonly   

新建DBHelpercs类
class DBHelpercs
{
public readonly static string StrConn = "Data Source=.;Initial Catalog=BoardroomManager;User ID=sa;pwd=123456";
}

//单机取消按钮
this.Close();

//单机清空按钮 foreach遍历所有文本框
foreach (var item in this.Controls)
{
//如果当前是一个TextBox控件
if (item is TextBox)
{
//清空所有文本框文本
TextBox txt = (TextBox)item;
txt.Text = "";
}
}

//点击提交方法
if (!YanZheng())
{
MessageBox.Show("所有项不能为空!请确认填写!");
return;
}
string datetime = this.txtDateTime.Text.Trim();
string room = this.txtRoom.Text.Trim();
string shiyou = this.txtShiYou.Text.Trim();
string name = this.txtName.Text.Trim();
string runame = this.txtLuRuName.Text.Trim();
SqlConnection conn = new SqlConnection(DBHelpercs.StrConn);
//sql语句
try
{
conn.Open();
string sql = "INSERT INTO [dbo].[DestineRecord]([DestineDate],[DestineMan],[DestineReason],[BoardRoomName],[NoteMan]) values(‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘)";
sql = string.Format(sql, datetime, room, shiyou, name, runame);
SqlCommand comd = new SqlCommand(sql, conn);
int count = Convert.ToInt32(comd.ExecuteNonQuery());
if (count > 0)
{
MessageBox.Show("添加成功!");
}
else
{
MessageBox.Show("添加失败!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}

//非空验证方法
public bool YanZheng()
{
bool succes = true;
//foreach遍历
foreach (var item in this.Controls)
{
if (item is TextBox)
{
if (((TextBox)item).Text.Trim().Equals(string.Empty))
{
succes = false;
break;
}
}
}
return succes; //返回true或false
}

//查询方法
public void Station()
{
string inputname = this.txtInputName.Text.Trim(); //获取输入
DataTable dt = new DataTable();
string sql = "SELECT [ID],[DestineDate] ,[DestineMan] ,[DestineReason],[BoardRoomName] ,[NoteMan]FROM [dbo].[DestineRecord]";
SqlDataAdapter ds = new SqlDataAdapter(sql, DBHelpercs.StrConn);
ds.Fill(dt);
this.dgv.DataSource=dt;
}

 

qwer

标签:empty   value   控件   inpu   exce   文本框   int   use   readonly   

原文地址:http://www.cnblogs.com/wangyuxin123/p/7738376.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!