public partial class PubtelCopy : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Session["UserName"] = User.Identity.Name.ToString(); //把用户名赋给session[“UserName”]
}
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue.ToString() == null) //如果组下拉旋框中,选择的组是空,就不让复制。
{
Response.Write("请选择要复制到的类别,然后才能复制。");
}
else
{
//选取类sql语句:把sql语句符合条件的记录放到记录集里,用myReader.GetString(1).ToString()来读取记录。
SqlConnection myconn;
myconn = new SqlConnection(ConfigurationManager.ConnectionStrings["BaseConnectionString"].ConnectionString);
myconn.Open();
SqlCommand myCMD = new SqlCommand("SELECT [PubtelID], [PubtelName], [PubtelContent], [RelationPerson], [RelationCompany], [Memo], [PubtelClassID], [fangjianhao] FROM [Pubtel] WHERE [PubtelID] = " + Request.QueryString["PubtelID"].ToString(), myconn);
SqlDataReader myReader = myCMD.ExecuteReader();
while (myReader.Read())
{
//判断所取得的字段值是否为空
string str1, str2, str3, str4, str5;
if (!myReader.IsDBNull(1)) { str1 = myReader.GetString(1).ToString(); }
else { str1 = ""; }
if (!myReader.IsDBNull(2)) { str2 = myReader.GetString(2).ToString(); }
else { str2 = ""; }
if (!myReader.IsDBNull(3)) { str3 = myReader.GetString(3).ToString(); }
else {str3="";}
if (!myReader.IsDBNull(4)) { str4 = myReader.GetString(4).ToString(); }
else { str4 = ""; }
if (!myReader.IsDBNull(5)) { str5 = myReader.GetString(5).ToString(); }
else { str5 = ""; }
//执行插入语句。属于执行型sql语句。
SqlConnection myconn1;
SqlCommand mycommand1;
myconn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["BaseConnectionString"].ConnectionString);
string sql1 = "INSERT INTO [Pertel] ([PertelName], [PertelContent], [RelationPerson], [RelationCompany], [Memo], [PertelClassID]) VALUES ('" + str1 + "', '" + str2+ "', '" + str3 + "', '" + str4 + "','" + str5 + "','" + DropDownList1.SelectedValue.ToString() + "')";
myconn1.Open();
mycommand1 = new SqlCommand(sql1, myconn1);
mycommand1.ExecuteNonQuery();
}
myReader.Close();
myconn.Close();
Response.Redirect("PubtelList.aspx");
}
}
}
注:1.把系统自带的函数把记录到的用户名,赋给session[“UserName”]: Session["UserName"] = User.Identity.Name.ToString();
2.取得文件中下拉选框的值: DropDownList1.SelectedValue.ToString()
3.判断从sql记录集中,所取得的字段值是否为空,代码如下:
string str1, str2, str3, str4, str5;
if (!myReader.IsDBNull(1)) //IsDBNull专门用来判断可能为NULL的字段,所以Get类函数前最好加这样一个判断。
{ str1 = myReader.GetString(1).ToString(); } //用GetString取值,但所有get类的函数,不能取得NULL,如果要取得值为NULL,则一定要用IsDBNull函数进行判断
Else
{ str1 = ""; }