惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
WordPress大学
WordPress大学
Help Net Security
Help Net Security
Jina AI
Jina AI
Security Latest
Security Latest
Y
Y Combinator Blog
Project Zero
Project Zero
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
Know Your Adversary
Know Your Adversary
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
NISL@THU
NISL@THU
Cisco Talos Blog
Cisco Talos Blog
博客园 - 司徒正美
MyScale Blog
MyScale Blog
Cyberwarzone
Cyberwarzone
D
Docker
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
C
CERT Recently Published Vulnerability Notes
B
Blog
L
LangChain Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
SecWiki News
SecWiki News
The Hacker News
The Hacker News
C
Check Point Blog
L
Lohrmann on Cybersecurity
V2EX - 技术
V2EX - 技术
S
Securelist
T
Threat Research - Cisco Blogs
Stack Overflow Blog
Stack Overflow Blog
TaoSecurity Blog
TaoSecurity Blog
云风的 BLOG
云风的 BLOG
Latest news
Latest news
人人都是产品经理
人人都是产品经理
L
LINUX DO - 最新话题
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The Register - Security
The Register - Security
Webroot Blog
Webroot Blog
Simon Willison's Weblog
Simon Willison's Weblog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Security Blog
Microsoft Security Blog
AWS News Blog
AWS News Blog
C
Cybersecurity and Infrastructure Security Agency CISA
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
小众软件
小众软件
T
Tailwind CSS Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
宝玉的分享
宝玉的分享
O
OpenAI News

博客园 - sin

2017年6月做的一个梦 开始写诗了 一张判断你是用左脑还是右脑的图 Javascript 刷新框架及页面的方法总集 - sin - 博客园 SQL高级查询技巧 自己写的SQL存储过程分页方法 ASP.NET刷新页面的六种方法 DataGrid传统分页方式 - sin - 博客园 ACCESS"数据库提示它已经被别的用户以独占方式打开,或没有查看数据的权限"的问题 - sin - 博客园 png图片在ie不透明的解决方案和浏览器通用的透明效果 div长度相等的几个办法 在IE浏览器中的奇怪页面表现 HTML标签大全 css杂问题 用css模拟title和alt的提示效果 用Web标准进行开发 div+css 面试题目 有什么有效方法清除多余的CSS代码 95%的中国网站需要重写CSS-写在px和em的区别下
.net简单添加数据库方法
sin · 2008-03-24 · via 博客园 - sin

  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