以下内容是关于C#处理SQL2000的数据的问题,求大佬帮帮忙~

用C#编写个程序能够对SQL中的表列的数据进行简单的查看,删除,增加。 
请高手帮我写个基础点的程序让我参考下,第一次碰到这题目! 
主要是C#是怎么与SQL中的表连接的请注明下,多谢!



隔江千里
浏览 274回答 3
3回答

幕布斯6054654

查询的语句: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("添加成功!");}

猛跑小猪

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语句

ITMISS

添加一个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绑定你想绑的空间就可以了 ~
打开App,查看更多内容
随时随地看视频慕课网APP