如何检查数据库uisng csharp中是否已存在Faculty代码 [英] how to check Faculty code is already exists in the datbase uisng csharp
本文介绍了如何检查数据库uisng csharp中是否已存在Faculty代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查记录已经存在;
我的保存代码如下;
private bool SaveDetails()
{
// 文本框验证不为空
bool 检查;
Check = true ;
if (txt_Faccode.Text.Trim()。Length == 0 )
{
Check = false ;
MessageBox.Show( 请输入学院代码, 不输入详细信息,MessageBoxButtons.OK,MessageBoxIcon.Information);
返回检查;
}
if (Txt_Facname.Text.Trim()。Length == 0 )
{
Check = false ;
MessageBox.Show( 请输入教员姓名, 不输入详细信息,MessageBoxButtons.OK,MessageBoxIcon.Information);
返回检查;
}
if (txt_Hrs.Text.Trim()。Length == 0 )
{
Check = false ;
MessageBox.Show( 请输入分配的小时数, 不输入详细信息,MessageBoxButtons.OK,MessageBoxIcon.Information);
返回检查;
}
// 插入代码以更新到DataBase
this .Cursor = Cursors.WaitCursor;
尝试
{
sql = 插入到Tb_SCH_Faculty_Details([Faculty_Code],[Faculty_Name],[Allocated_Hours]) + values(' + txt_Faccode.Text.ToUpper()+ ',' + Txt_Facname.Text + ', + txt_Hrs.Text + );
int temp = 0 ;
if (!int.TryParse(txt_Hrs.Text.Trim(), out temp) )
{
Check = false ;
this .Cursor = Cursors.Arrow;
MessageBox.Show( 仅在分配的小时内输入数字, 字符不允许,MessageBoxButtons.OK,MessageBoxIcon.Error);
返回检查;
}
GFun.Error = ;
GFun.InsertAccessData(sql);
if (GFun.Error.ToString()!= )
{
Check = false ;
MessageBox.Show(GFun.Error.ToString(), 错误);
this .Cursor = Cursors.Arrow;
返回检查;
}
GFun.OleDbCon.Close();
}
catch (例外情况)
{
Check = false 跨度>;
MessageBox.Show(ex.ToString(), 错误,MessageBoxButtons.OK, MessageBoxIcon.Error);
this .Cursor = Cursors.Arrow;
返回检查;
}
this .Cursor = Cursors.Arrow;
返回检查;
}
私有 void Btn_Save_Click( object sender,EventArgs e)
{
if (SaveDetails()== true )
{
txt_Faccode.Text = ;
Txt_Facname.Text = ;
txt_Hrs.Text = ;
LoadFacultyDetails();
MessageBox.Show( 记录已成功插入, 记录已插入,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
当我点击保存按钮记录保存成功。工作正常。
但我再次使用相同的教师代码并单击保存按钮。我想显示教师代码已经存在的消息。
我怎么能用csharp。
注意:它是windows应用程序。
请帮帮我。
解决方案
尝试如下。
string cmdStr = 从Tb_SCH_Faculty_Details中选择count(*),其中Faculty_Code =' + txt_Faccode.Text.ToUpper()+ '; // 将记录存在为计数
OleDbCommand cmd = < span class =code-keyword> new OleDbCommand(cmdStr,conn);
int count =( int )cmd.ExecuteScalar();
if (count> 0)
{
// 记录已存在
MessageBox.Show( 记录存在);
}
其他
{
// < span class =code-comment>插入记录。
}
希望有帮助
how to check record already exists;
My save code as follows;
private bool SaveDetails()
{
//validation for textbox not empty
bool Check;
Check = true;
if (txt_Faccode.Text.Trim().Length == 0)
{
Check = false;
MessageBox.Show("Please enter the Faculty code","Not Enter the details",MessageBoxButtons.OK,MessageBoxIcon.Information);
return Check;
}
if (Txt_Facname.Text.Trim().Length == 0)
{
Check = false;
MessageBox.Show("Please enter the Faculty Name","Not Enter the details",MessageBoxButtons.OK,MessageBoxIcon.Information);
return Check;
}
if (txt_Hrs.Text.Trim().Length == 0)
{
Check = false;
MessageBox.Show("Please enter the allocated hours","Not Enter the details",MessageBoxButtons.OK,MessageBoxIcon.Information);
return Check;
}
//insert code for update to the DataBase
this.Cursor = Cursors.WaitCursor;
try
{
sql = "insert into Tb_SCH_Faculty_Details ([Faculty_Code], [Faculty_Name],[Allocated_Hours]) " + " values('" + txt_Faccode.Text.ToUpper() + "','" + Txt_Facname.Text + "', " + txt_Hrs.Text + ")";
int temp = 0;
if (!int.TryParse(txt_Hrs.Text.Trim(), out temp))
{
Check = false;
this.Cursor = Cursors.Arrow;
MessageBox.Show("Enter Numbers only in allocated hours", "Characters Not Allowed", MessageBoxButtons.OK, MessageBoxIcon.Error);
return Check;
}
GFun.Error = "";
GFun.InsertAccessData(sql);
if (GFun.Error.ToString() != "")
{
Check = false;
MessageBox.Show(GFun.Error.ToString(), "Error");
this.Cursor = Cursors.Arrow;
return Check;
}
GFun.OleDbCon.Close();
}
catch (Exception ex)
{
Check = false;
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Cursor = Cursors.Arrow;
return Check;
}
this.Cursor = Cursors.Arrow;
return Check;
}
private void Btn_Save_Click(object sender, EventArgs e)
{
if (SaveDetails() == true)
{
txt_Faccode.Text = "";
Txt_Facname.Text = "";
txt_Hrs.Text = "";
LoadFacultyDetails();
MessageBox.Show("Record Inserted Successfully", "Records Inserted", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
when i click the save button records saved successfully. working fine.
but again i ente the same faculty code and click the save button. i want to show the message the faculty code is already exists.
for that how can i do using csharp.
Note : it is windows application.
please help me.
解决方案
Hi,
try like below.
string cmdStr = "Select count(*) from Tb_SCH_Faculty_Details where Faculty_Code = '" + txt_Faccode.Text.ToUpper() + "'"; //get the existence of the record as count OleDbCommand cmd = new OleDbCommand(cmdStr, conn); int count = (int)cmd.ExecuteScalar(); if(count >0) { //record already exist MessageBox.Show("Record Exists"); } else { //insert the record. }
hope it helps
这篇关于如何检查数据库uisng csharp中是否已存在Faculty代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文