亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

以下內容是關于C#處理SQL2000的數據的問題,求大佬幫幫忙~

以下內容是關于C#處理SQL2000的數據的問題,求大佬幫幫忙~

C# C
隔江千里 2021-12-02 20:07:11
用C#編寫個程序能夠對SQL中的表列的數據進行簡單的查看,刪除,增加。 請高手幫我寫個基礎點的程序讓我參考下,第一次碰到這題目! 主要是C#是怎么與SQL中的表連接的請注明下,多謝!
查看完整描述

3 回答

?
幕布斯6054654

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("添加成功!");
}



查看完整回答
反對 回復 2021-12-06
?
猛跑小豬

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

查看完整回答
反對 回復 2021-12-06
?
ITMISS

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綁定你想綁的空間就可以了 ~



查看完整回答
反對 回復 2021-12-06
  • 3 回答
  • 0 關注
  • 296 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號