ADO.net增刪改的使用

來源:互聯網
上載者:User

標籤:

 

添加資料

--------------------------------------------------

//讓使用者輸入要添加的內容
Console.WriteLine("請輸入要添加的代號:");
string code = Console.ReadLine();

Console.WriteLine("請輸入姓名:");
string name = Console.ReadLine();

Console.WriteLine("請輸入性別:");
bool sex = Console.ReadLine()=="男"?true:false;

Console.WriteLine("請輸入民族:");
string nation = Console.ReadLine();

Console.WriteLine("請輸入生日:");
string birthday = Console.ReadLine();

string nationcode = "n001";

//將民族名稱轉為名族代號
SqlConnection conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123");
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select Code from Nation where Name = ‘"+nation+"‘";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
nationcode = dr[0].ToString();
}
conn.Close();

//往Info表添加資料
cmd.CommandText = "insert into Info values(‘"+code+"‘,‘"+name+"‘,‘"+sex+"‘,‘"+nationcode+"‘,‘"+birthday+"‘)";
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Console.WriteLine("添加成功!");

Console.ReadLine();

------------------------------------------------------

刪除資料

----------------------------------------------------------------

//使用者輸入要刪除的資料主索引值
Console.WriteLine("請輸入要刪除的代號:");
string code = Console.ReadLine();

//判斷該資料存不存在
SqlConnection conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123");
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from Info where Code=‘"+code+"‘";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();

if (dr.HasRows)
{
//說明該資料存在
Console.WriteLine("查到該資料,是否要執行刪除操作,如果要刪除請輸入:1");
int sc = Convert.ToInt32(Console.ReadLine());

if (sc == 1)
{
//刪除
dr.Close(); //關閉讀取器

cmd.CommandText = "delete from Info where Code=‘"+code+"‘";
cmd.ExecuteNonQuery();
Console.WriteLine("刪除成功!");

}
else
{
//不刪除
dr.Read();

string sex = Convert.ToBoolean(dr[2])?"男":"女";
string nation = MinZu(dr[3].ToString());

string str = "代號:"+dr[0]+"\t姓名:"+dr[1]+"\t性別:"+sex+"\t民族:"+nation+"\t生日:"+dr[4];

Console.WriteLine(str);


}
}
else
{
//資料不存在
Console.WriteLine("輸入的代號錯誤!");
}

conn.Close();
Console.ReadLine();
}


static string MinZu(string code)
{
string name="";
SqlConnection conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123");
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select Name from Nation where Code = ‘" + code + "‘";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
name = dr[0].ToString();
}
conn.Close();

return name;

----------------------------------------------------------------

修改資料

將cmd.commandtext語句改為需要update語句即可

ADO.net增刪改的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.