现在的位置: 首页 > 综合 > 正文

.net简单添加数据库方法

2012年09月14日 ⁄ 综合 ⁄ 共 7760字 ⁄ 字号 评论关闭

 

  1 using System;
  2 using System.Data;
  3 using System.Configuration;
  4 using System.Web;
  5 using System.Web.Security;
  6 using System.Web.UI;
  7 using System.Web.UI.WebControls;
  8 using System.Web.UI.WebControls.WebParts;
  9 using System.Web.UI.HtmlControls;
 10 using System.Data.OleDb;
 11 using System.Data.SqlClient;
 12 
 13 
 14 public partial class _Default : System.Web.UI.Page 
 15 {
 16     //public string connection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + System.Web.HttpContext.Current.Server.MapPath("~/App_Data/db1.mdb") + ";";//连接access
 17 
 18     public static SqlConnection conn;
 19     public string connection1 = "UID=sa;pwd=;database=mysql";
 20     public string name1 = "", sex1 = "", zhuanye1 = "", aihao11 = "", aihao21 = "", aihao31 = "", jianjie1="";
 21     public string connection = "Provider=SQLOLEDB;Data sourse=127.0.0.1;UID=sa;pwd=;database=mysql";      //连接 SQL
 22 
 23     protected void Page_Load(object sender, EventArgs e)
 24     {
 25         if(!IsCallback){
 26 
 27         string strSQL = "select * FROM [user1]";
 28 
 29         OleDbDataAdapter objCmd = new OleDbDataAdapter(strSQL, connection);
 30         DataSet ds = new DataSet();
 31         objCmd.Fill(ds);
 32         GridView1.DataSource = ds.Tables[0];
 33         GridView1.DataBind();
 34         }
 35         if (!string.IsNullOrEmpty(Request["id"]))
 36         {
 37             id.Value = Request["id"].ToString();
 38             string sql = "select * from [user1] where id="+Request["id"].ToString();
 39             OleDbDataAdapter cmd = new OleDbDataAdapter(sql,connection);
 40             DataTable dt = new DataTable();
 41             cmd.Fill(dt);
 42             name.Text = dt.Rows[0]["name"].ToString();
 43             if (dt.Rows[0]["sex"].ToString() == "True")
 44             {
 45                 sex.SelectedIndex = 0;
 46             }
 47             else
 48             {
 49                 sex.SelectedIndex = 1;
 50             }
 51             switch (dt.Rows[0]["zhuanye"].ToString())
 52             {
 53                 case "计算器":
 54                     zhuanye.SelectedIndex = 1;
 55                     break;
 56                 case "软件":
 57                     zhuanye.SelectedIndex = 2;
 58                     break;
 59             }
 60             if (dt.Rows[0]["aihao1"].ToString()==aihao1.Text)
 61             {
 62                 aihao1.Checked = true;
 63             }
 64 
 65             if (dt.Rows[0]["aihao2"].ToString() == aihao2.Text)
 66             {
 67                 aihao2.Checked = true;
 68             }
 69             if (dt.Rows[0]["aihao3"].ToString() == aihao3.Text)
 70             {
 71                 aihao3.Checked = true;
 72             }
 73             jianjie.Text = dt.Rows[0]["jianjie"].ToString();
 74         }
 75 
 76         conn = new SqlConnection(connection1);
 77         SqlCommand cmd1 = new SqlCommand("page", conn);
 78         cmd1.CommandType = CommandType.StoredProcedure;
 79 
 80         SqlParameter prmReturn = new SqlParameter("@count", SqlDbType.Int);
 81         prmReturn.Direction = ParameterDirection.Output;
 82         cmd1.Parameters.Add(prmReturn);
 83 
 84         prmReturn = new SqlParameter("@PageCount", SqlDbType.Int);
 85         prmReturn.Direction = ParameterDirection.Output;
 86         cmd1.Parameters.Add(prmReturn);
 87 
 88         SqlParameter psex = cmd1.Parameters.Add("@PageIndex", SqlDbType.Int);
 89         psex.Direction = ParameterDirection.Input;
 90         psex.Value = 30;
 91         SqlDataAdapter ds0 = new SqlDataAdapter(cmd1);
 92         DataSet ds1 = new DataSet();
 93         ds0.Fill(ds1);
 94         GridView1.DataSource = ds1;
 95         GridView1.DataBind();
 96 
 97         conn.Open();
 98         cmd1.ExecuteNonQuery();
 99         conn.Close();
100 
101         count.Text = cmd1.Parameters["@PageIndex"].Value.ToString();
102        PageCount.Text = cmd1.Parameters["@PageCount"].Value.ToString();
103 
104     }
105     protected void Button1_Click(object sender, EventArgs e)
106     {
107 
108             name1 = name.Text.ToString();
109             if (sex.Text.ToString() == "1")
110             {
111                 sex1 = "1";
112             }
113             else
114             {
115                 sex1 = "0";
116             }
117             zhuanye1 = zhuanye.Text;
118             aihao11 = aihao1.Text.ToString();
119             aihao21 = aihao2.Text.ToString();
120             aihao31 = aihao3.Text.ToString();
121             jianjie1 = jianjie.Text.ToString();
122 
123             string str = "INSERT INTO [user1](name,sex,zhuanye,aihao1,aihao2,aihao3,jianjie) VALUES ('" + name1 + "'," + sex1 + ",'" + zhuanye1 + "','" + aihao11 + "','" + aihao21 + "','" + aihao31 + "','" + jianjie1 + "')";
124 
125             OleDbConnection conn = new OleDbConnection(connection);
126             OleDbCommand cmd = new OleDbCommand(str, conn);
127             cmd.CommandType = CommandType.Text;
128             conn.Open();
129             cmd.ExecuteNonQuery();
130             conn.Close();
131 
132         Response.Redirect("Default.aspx");
133     }
134     protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
135     {
136         int id = int.Parse(e.CommandArgument.ToString());
137         if(e.CommandName.ToString()=="edit"){
138             Response.Redirect("Default.aspx?id="+id.ToString());
139         }
140     }
141     protected void Button2_Click(object sender, EventArgs e)
142     {
143         name1 = Request["name"].ToString();
144         if (Request["sex"].ToString() == "True")
145         {
146             sex1 = "1";
147         }
148         else
149         {
150             sex1 = "0";
151         }
152         zhuanye1 =Request["zhuanye"].ToString();
153         if (!string.IsNullOrEmpty(Request["aihao1"]))
154         {
155             aihao11 = "打球";
156         }
157         if (!string.IsNullOrEmpty(Request["aihao2"]))
158         {
159             aihao21 = "游泳";
160         }
161         if (!string.IsNullOrEmpty(Request["aihao3"]))
162         {
163             aihao31 = "打机";
164         }
165         jianjie1 = Request["jianjie"].ToString();
166         string str = "update [user1] set name='" + name1 + "',sex=" + sex1 + ",zhuanye='" + zhuanye1 + "',aihao1='" + aihao11 + "',aihao2='" + aihao21 + "',aihao3='" + aihao31 + "',jianjie='" + jianjie1 + "' where id=" + Request["id"].ToString();
167 
168         OleDbConnection conn = new OleDbConnection(connection);
169         OleDbCommand cmd = new OleDbCommand(str,conn);
170         cmd.CommandType = CommandType.Text;
171         conn.Open();
172         cmd.ExecuteNonQuery();
173         conn.Close();
174         Response.Redirect("Default.aspx");
175         }
176     public string gettyle1(string str)
177     {
178         if (str == "True")
179         {
180             return "";
181         }
182         else
183         {
184             return "";
185         }
186     }
187     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
188     {
189         string sexa = DropDownList1.SelectedValue;
190         conn = new SqlConnection(connection1);
191         SqlCommand cmd = new SqlCommand("selectsex", conn);
192         cmd.CommandType = CommandType.StoredProcedure;
193         SqlParameter psex = cmd.Parameters.Add("@sex", SqlDbType.NChar);
194         psex.Direction = ParameterDirection.Input;
195         psex.Value = sexa;
196         SqlDataAdapter ds = new SqlDataAdapter(cmd);
197         DataSet ds1 = new DataSet();
198         ds.Fill(ds1);
199         GridView1.DataSource = ds1;
200         GridView1.DataBind();
201 
202         //prmReturn = new SqlParameter("@iReturn", SqlDbType.Int);
203         //prmReturn.Direction = ParameterDirection.ReturnValue;
204         //m_objCmd.Parameters.Add(prmReturn);
205 
206     }
207 }
208 

抱歉!评论已关闭.