3 回答
TA贡献1876条经验 获得超7个赞
查询的语句:
string strConn = "Data Source=.;Initial Catalog=JJking;uid=sa;pwd=12345"; //SQL语言,连接数据库
sqlConn.Open(); //打开数据库
daEmployees = new SqlDataAdapter("select cEmpId,vEmpName,vAddress,vTelephone,dJoinDate from dbo.Employees where cEmpId='" + strId + "' and dResignationDate is null", sqlConn); //找到相对应的表
dsEmployees = new DataSet();
daEmployees.Fill(dsEmployees, "表的名字");
dgvEmployees.DataSource = dsEmployees.Tables["表的名字"];
string strCommand = "select * from dbo.Employees where cEmpId='" + strId + "' and dResignationDate is null";
SqlCommand sqlComm = new SqlCommand(strCommand, sqlConn);
SqlDataReader sqlDr = sqlComm.ExecuteReader();
sqlConn.Close();
删除的:
SqlConnection sqlConn = new SqlConnection(strConn); //连接数据库的语言上面有
SqlConnection sqlConn = new SqlConnection(strConn);
string strId = txtId.Text.Trim(); //定义变量
string strName = txtName.Text.Trim();
string strAddress = txtAddress.Text.Trim();
string strTelephone = txtTelephone.Text.Trim();
string strJoinDate = txtJoinDate.Text.Trim();
string strResignationDate = txtResignationDate.Text.Trim();
try
{
sqlConn.Open();
string strCommand = "update dbo.Employees set dResignationDate='" + strResignationDate + "' where cEmpId='" + strId + "'";
SqlCommand sqlComm = new SqlCommand(strCommand, sqlConn);
int i = (int)sqlComm.ExecuteNonQuery();
if (i == 1)
{
daEmployees = new SqlDataAdapter("select * from dbo.Employees where dResignationDate is null", sqlConn);
dsEmployees = new DataSet();
daEmployees.Fill(dsEmployees, "Employees");
dgvEmployees.DataSource = dsEmployees.Tables["Employees"];
MessageBox.Show("删除成功!");
}
写晕了。。。简单点。。。
SqlConnection sqlConn = new SqlConnection(strConn);
string strCommand = "insert into dbo.Employees(cEmpId,vEmpName,vAddress,vTelephone,dJoinDate,dResignationDate) values('" + strId + "','" + strName + "','" + strAddress + "','" + strTelephone + "','" + strJoinDate + "','" + strResignationDate + "')"; //cEmpId,vEmpName,vAddress....和数据库中表的列对应
SqlCommand sqlComm = new SqlCommand(strCommand, sqlConn);
int i = (int)sqlComm.ExecuteNonQuery();
if (i == 1)
{
daEmployees = new SqlDataAdapter("select * from dbo.Employees ", sqlConn);
dsEmployees = new DataSet();
daEmployees.Fill(dsEmployees, "Employees");
dgvEmployees.DataSource = dsEmployees.Tables["Employees"];
MessageBox.Show("添加成功!");
}
TA贡献1858条经验 获得超8个赞
string connectionString="Data Source=服务器;Initial Catalog=数据库名;Integrated Security=True";//连接字符串
SqlConnection connection = new SqlConnection(connectionstring);
connection.Open();//打开数据库连接
string sql= "";//SQL语句
SqlCommand command = new SqlCommand(sql, connection);//执行数据库命令
command就是你得到的结果,至于类型取决于你的SQL语句
TA贡献1871条经验 获得超8个赞
添加一个web.config文件,在里面添加
<appSettings>
<add key="SqlConnStr" value="server=(local);uid=sa;pwd=123;database=hotels"/>
</appSettings>
key 是调用该链接的名称 value是连接的的值
server 服务器地址 uid 用户名 pwd 密码 database 数据库名称
using System.Configuration; //记得引用这个包
private static string Connstr =ConfigurationManager.AppSettings["SqlConnStr"].ToString();//获取连接字符串
sqlconnection con=new sqlconnection(conStr);
con.Open();//打开数据库连接
string sql1="select * from 表名"; 这个是查看
string sql2="insert into 表名(列名,列名)values(插入的值,插入的值)";这个是增加
string sql3="delete from 表名 where 列名=值";删除
sqlcommand com=new sqlcommand(要执行的sql语句,con);//这个是用来增加,删除的
com.ExecuteNonQuery();//执行
SqlDataAdapter da = new SqlDataAdapter(sql1,con);//查询
dataset ds=new dataset();
da.fill(ds);
然后用ds绑定你想绑的空间就可以了 ~
- 3 回答
- 0 关注
- 272 浏览
添加回答
举报