C#和SQL Server2000数据库连接,查询语句总结
2007-05-31 09:55
811 查看
using System.Data;
using System.Data.SqlClient;
private SqlConnection myconn;
private SqlCommand mycomm;
一.单表
try
{
//插入记录
this.myconn=new SqlConnection();
this.myconn.ConnectionString= "workstation id=3BB9B810500D4AC;packet size=4096;integrated security=SSPI;data sou" + "rce=3BB9B810500D4AC;persist security info=False;initial catalog=Maintenance";
this.myconn.Open();
string insertString = string.Format(
"INSERT INTO salary VALUES ('{0}','{1}','{2}',{3},{4},'{5}')"
,number++,workerName,time,salary,prize,details);
this.mycomm=new SqlCommand();
this.mycomm.CommandText=insertString;
this.mycomm.Connection=this.myconn;
this.mycomm.ExecuteNonQuery();
MessageBox.Show(this,"员工工资的信息录入成功!","提示信息",
MessageBoxButtons.OK,MessageBoxIcon.Information);
this.txtSalary.Text="";
this.txtPrize.Text="";
this.txtDetails.Text="";
}
catch
{
MessageBox.Show(this,"员工工资的信息保存失败!","提示信息",
MessageBoxButtons.OK,MessageBoxIcon.Information);
}
finally
{
this.myconn.Close();
}
//修改记录
try
{
this.myconn=new SqlConnection();
this.myconn.ConnectionString= "workstation id=3BB9B810500D4AC;packet size=4096;integrated security=SSPI;data sou" +
"rce=3BB9B810500D4AC;persist security info=False;initial catalog=Maintenance";
this.myconn.Open();
string updateString="UPDATE salary SET 基本工资="+salary+",奖金="+prize+",备注='"+details+"' WHERE 员工姓名='"+workerName+"' AND 年月份='"+time+"'";
this.mycomm=new SqlCommand();
this.mycomm.CommandText=updateString;
this.mycomm.Connection=this.myconn;
this.mycomm.ExecuteNonQuery();
MessageBox.Show(this,"员工工资的信息保存成功!","提示信息",
MessageBoxButtons.OK,MessageBoxIcon.Information);
this.btnSaveUpdate.Enabled=false;
this.txtSalary.Text="";
this.txtPrize.Text="";
this.txtDetails.Text="";
}
catch
{
MessageBox.Show(this,"员工工资的信息保存失败!","提示信息",
MessageBoxButtons.OK,MessageBoxIcon.Information);
}
finally
{
this.myconn.Close();
}
//删除信息
if(result==DialogResult.OK)
{
try
{
string deleteString=String.Format("DELETE FROM worker_archives WHERE 员工姓名='{0}'",workerName);
this.myConn=new SqlConnection();
this.myConn.ConnectionString= "workstation id=3BB9B810500D4AC;packet size=4096;integrated security=SSPI;data sou" +
"rce=3BB9B810500D4AC;persist security info=False;initial catalog=Maintenance";
this.myConn.Open();
myComm=new SqlCommand();
myComm.Connection=this.myConn;
myComm.CommandText=deleteString;
myComm.ExecuteNonQuery();
this.rbBoy.Checked=false;
this.rbGirl.Checked=false;
this.txtAge.Text="";
this.txtAddress.Text="";
this.txtPhone.Text="";
this.txtMobilePhone.Text="";
this.txtStatus.Text="";
this.txtEmail.Text="";
}
finally
{
this.myConn.Close();
}
二.多表
//多表查询
string cmdText="SELECT RealName,Role_Name,Role_Description FROM Staff "
+"INNER JOIN StaffRole ON Staff.Staff_ID=StaffRole.Staff_ID "
+"INNER JOIN Role ON Role.Role_ID=StaffRole.Role_ID "
+"WHERE Staff.Staff_Name="
+"'"+sUserName+"'";
SqlConnection myConn=new SqlConnection(STRCONNECTION);
SqlCommand myComm=new SqlCommand(cmdText,myConn);
myConn.Open();
SqlDataReader dr=myComm.ExecuteReader();
string result="真实姓名为:";
if(dr.Read())
{
result+=dr["RealName"].ToString();
result+="<br>用户角色名为:";
result+=dr["Role_Name"].ToString();
result+="<br>角色描述为:";
result+=dr["Role_Description"].ToString();
result+="<br>";
}
else
{
result+="<br>用户角色为:";
result+="<br>角色描述为:";
}
dr.Close();
myConn.Close();
using System.Data.SqlClient;
private SqlConnection myconn;
private SqlCommand mycomm;
一.单表
try
{
//插入记录
this.myconn=new SqlConnection();
this.myconn.ConnectionString= "workstation id=3BB9B810500D4AC;packet size=4096;integrated security=SSPI;data sou" + "rce=3BB9B810500D4AC;persist security info=False;initial catalog=Maintenance";
this.myconn.Open();
string insertString = string.Format(
"INSERT INTO salary VALUES ('{0}','{1}','{2}',{3},{4},'{5}')"
,number++,workerName,time,salary,prize,details);
this.mycomm=new SqlCommand();
this.mycomm.CommandText=insertString;
this.mycomm.Connection=this.myconn;
this.mycomm.ExecuteNonQuery();
MessageBox.Show(this,"员工工资的信息录入成功!","提示信息",
MessageBoxButtons.OK,MessageBoxIcon.Information);
this.txtSalary.Text="";
this.txtPrize.Text="";
this.txtDetails.Text="";
}
catch
{
MessageBox.Show(this,"员工工资的信息保存失败!","提示信息",
MessageBoxButtons.OK,MessageBoxIcon.Information);
}
finally
{
this.myconn.Close();
}
//修改记录
try
{
this.myconn=new SqlConnection();
this.myconn.ConnectionString= "workstation id=3BB9B810500D4AC;packet size=4096;integrated security=SSPI;data sou" +
"rce=3BB9B810500D4AC;persist security info=False;initial catalog=Maintenance";
this.myconn.Open();
string updateString="UPDATE salary SET 基本工资="+salary+",奖金="+prize+",备注='"+details+"' WHERE 员工姓名='"+workerName+"' AND 年月份='"+time+"'";
this.mycomm=new SqlCommand();
this.mycomm.CommandText=updateString;
this.mycomm.Connection=this.myconn;
this.mycomm.ExecuteNonQuery();
MessageBox.Show(this,"员工工资的信息保存成功!","提示信息",
MessageBoxButtons.OK,MessageBoxIcon.Information);
this.btnSaveUpdate.Enabled=false;
this.txtSalary.Text="";
this.txtPrize.Text="";
this.txtDetails.Text="";
}
catch
{
MessageBox.Show(this,"员工工资的信息保存失败!","提示信息",
MessageBoxButtons.OK,MessageBoxIcon.Information);
}
finally
{
this.myconn.Close();
}
//删除信息
if(result==DialogResult.OK)
{
try
{
string deleteString=String.Format("DELETE FROM worker_archives WHERE 员工姓名='{0}'",workerName);
this.myConn=new SqlConnection();
this.myConn.ConnectionString= "workstation id=3BB9B810500D4AC;packet size=4096;integrated security=SSPI;data sou" +
"rce=3BB9B810500D4AC;persist security info=False;initial catalog=Maintenance";
this.myConn.Open();
myComm=new SqlCommand();
myComm.Connection=this.myConn;
myComm.CommandText=deleteString;
myComm.ExecuteNonQuery();
this.rbBoy.Checked=false;
this.rbGirl.Checked=false;
this.txtAge.Text="";
this.txtAddress.Text="";
this.txtPhone.Text="";
this.txtMobilePhone.Text="";
this.txtStatus.Text="";
this.txtEmail.Text="";
}
finally
{
this.myConn.Close();
}
二.多表
//多表查询
string cmdText="SELECT RealName,Role_Name,Role_Description FROM Staff "
+"INNER JOIN StaffRole ON Staff.Staff_ID=StaffRole.Staff_ID "
+"INNER JOIN Role ON Role.Role_ID=StaffRole.Role_ID "
+"WHERE Staff.Staff_Name="
+"'"+sUserName+"'";
SqlConnection myConn=new SqlConnection(STRCONNECTION);
SqlCommand myComm=new SqlCommand(cmdText,myConn);
myConn.Open();
SqlDataReader dr=myComm.ExecuteReader();
string result="真实姓名为:";
if(dr.Read())
{
result+=dr["RealName"].ToString();
result+="<br>用户角色名为:";
result+=dr["Role_Name"].ToString();
result+="<br>角色描述为:";
result+=dr["Role_Description"].ToString();
result+="<br>";
}
else
{
result+="<br>用户角色为:";
result+="<br>角色描述为:";
}
dr.Close();
myConn.Close();
相关文章推荐
- C# 简单连接数据库并执行SQL查询语句
- 关于过去工作的一些总结:常用sql语句以及数据库查询优化
- 数据库质疑修复总结 For SQL Server 2000/2005/2008/2008R2
- Delphi使用BDE连接远程MS SQL SERVER 2000数据库时出现“Db-library network communciations layer not loaded.”错误的解决方法
- 最强的sql server中查看连接数据的查询语句
- C#不同数据库连接方式对应的Sql语句的构造
- C#(.net)中的一次连接数据库执行多条sql语句
- 数据查询基础和C#连接数据库语句
- C#中如何连接数据库并执行SQL语句
- SQL语句远程连接其他数据库查询表数据
- 关于数据库查询语句SqlDataReader的连接释放问题的解决办法
- C#连接数据库增删改查代码 SQL SERVER/ACCESS 通用类
- 巧用数据库SQL语句的“连接查询”功能
- C#连接数据库最基本操作之sql语句 DML
- 数据库SQL语言语法总结3---查询语句
- 在Windows Server 2008 R2 Server中,连接其他服务器的数据库遇到“未启用当前数据库的 SQL Server Service Broker,因此查询通知不受支持。如果希望使用通知,请为此数据库启用 Service Broker ”
- asp.net with c#连接sql 2000数据库
- Delphi使用BDE连接远程MS SQL SERVER 2000数据库时出现“Db-library network communciations layer not loaded.”错误的解决方法
- 工作总结之PHP连接Microsoft SQL Server 2000/2005/2008
- 水晶报表连接MS-SQL-Server 2000的经验总结