时间:2021-07-01 10:21:17 帮助过:9人阅读
- <span style="color: #008080">1</span> <span style="color: #0000ff"><</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">></span>
- <span style="color: #008080">2</span> <span style="color: #0000ff"><</span><span style="color: #800000">add </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="connStr"</span><span style="color: #ff0000"> connectionString</span><span style="color: #0000ff">="server=.;uid=sa;pwd=****;database=****;Charset=utf8"</span><span style="color: #0000ff">/></span>
- <span style="color: #008080">3</span> <span style="color: #0000ff"></</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">></span>
SqlHelper
- <span style="color: #008080"> 1</span> <span style="color: #0000ff">using</span><span style="color: #000000"> System;
- </span><span style="color: #008080"> 2</span> <span style="color: #0000ff">using</span><span style="color: #000000"> System.Collections.Generic;
- </span><span style="color: #008080"> 3</span> <span style="color: #0000ff">using</span><span style="color: #000000"> System.Configuration;
- </span><span style="color: #008080"> 4</span> <span style="color: #0000ff">using</span><span style="color: #000000"> System.Data;
- </span><span style="color: #008080"> 5</span> <span style="color: #0000ff">using</span><span style="color: #000000"> System.Data.SqlClient;
- </span><span style="color: #008080"> 6</span> <span style="color: #0000ff">using</span><span style="color: #000000"> System.Linq;
- </span><span style="color: #008080"> 7</span> <span style="color: #0000ff">using</span><span style="color: #000000"> System.Text;
- </span><span style="color: #008080"> 8</span> <span style="color: #0000ff">using</span><span style="color: #000000"> System.Threading.Tasks;
- </span><span style="color: #008080"> 9</span>
- <span style="color: #008080">10</span> <span style="color: #0000ff">namespace</span><span style="color: #000000"> ConsoleApplication1
- </span><span style="color: #008080">11</span> <span style="color: #000000">{
- </span><span style="color: #008080">12</span> <span style="color: #0000ff">class</span><span style="color: #000000"> SqlHelper
- </span><span style="color: #008080">13</span> <span style="color: #000000"> {
- </span><span style="color: #008080">14</span> <span style="color: #008000">//</span><span style="color: #008000">ADO.net中的SqlHelper</span>
- <span style="color: #008080">15</span>
- <span style="color: #008080">16</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">readonly</span> <span style="color: #0000ff">string</span> connStr = ConfigurationManager.ConnectionStrings[<span style="color: #800000">"</span><span style="color: #800000">connStr</span><span style="color: #800000">"</span><span style="color: #000000">].ConnectionString;
- </span><span style="color: #008080">17</span>
- <span style="color: #008080">18</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span><span style="color: #000000"> SqlConnection CreateConnection()
- </span><span style="color: #008080">19</span> <span style="color: #000000"> {
- </span><span style="color: #008080">20</span> SqlConnection conn = <span style="color: #0000ff">new</span><span style="color: #000000"> SqlConnection(connStr);
- </span><span style="color: #008080">21</span> <span style="color: #000000"> conn.Open();
- </span><span style="color: #008080">22</span> <span style="color: #0000ff">return</span><span style="color: #000000"> conn;
- </span><span style="color: #008080">23</span> <span style="color: #000000"> }
- </span><span style="color: #008080">24</span>
- <span style="color: #008080">25</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> ExecuteNonQuery(SqlConnection conn, <span style="color: #0000ff">string</span> sql, <span style="color: #0000ff">params</span><span style="color: #000000"> SqlParameter[] parameters)
- </span><span style="color: #008080">26</span> <span style="color: #000000"> {
- </span><span style="color: #008080">27</span> <span style="color: #0000ff">using</span> (SqlCommand cmd =<span style="color: #000000"> conn.CreateCommand())
- </span><span style="color: #008080">28</span> <span style="color: #000000"> {
- </span><span style="color: #008080">29</span> cmd.CommandText =<span style="color: #000000"> sql;
- </span><span style="color: #008080">30</span> <span style="color: #000000"> cmd.Parameters.AddRange(parameters);
- </span><span style="color: #008080">31</span> <span style="color: #0000ff">return</span><span style="color: #000000"> cmd.ExecuteNonQuery();
- </span><span style="color: #008080">32</span> <span style="color: #000000"> }
- </span><span style="color: #008080">33</span> <span style="color: #000000"> }
- </span><span style="color: #008080">34</span>
- <span style="color: #008080">35</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> ExecuteNonQuery(<span style="color: #0000ff">string</span> sql, <span style="color: #0000ff">params</span><span style="color: #000000"> SqlParameter[] parameters)
- </span><span style="color: #008080">36</span> <span style="color: #000000"> {
- </span><span style="color: #008080">37</span> <span style="color: #0000ff">using</span> (SqlConnection conn =<span style="color: #000000"> CreateConnection())
- </span><span style="color: #008080">38</span> <span style="color: #000000"> {
- </span><span style="color: #008080">39</span> <span style="color: #0000ff">return</span><span style="color: #000000"> ExecuteNonQuery(conn, sql, parameters);
- </span><span style="color: #008080">40</span> <span style="color: #000000"> }
- </span><span style="color: #008080">41</span> <span style="color: #000000"> }
- </span><span style="color: #008080">42</span>
- <span style="color: #008080">43</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">object</span> ExecuteScalar(SqlConnection conn, <span style="color: #0000ff">string</span> sql, <span style="color: #0000ff">params</span><span style="color: #000000"> SqlParameter[] parameters)
- </span><span style="color: #008080">44</span> <span style="color: #000000"> {
- </span><span style="color: #008080">45</span> <span style="color: #0000ff">using</span> (SqlCommand cmd =<span style="color: #000000"> conn.CreateCommand())
- </span><span style="color: #008080">46</span> <span style="color: #000000"> {
- </span><span style="color: #008080">47</span> cmd.CommandText =<span style="color: #000000"> sql;
- </span><span style="color: #008080">48</span> <span style="color: #000000"> cmd.Parameters.AddRange(parameters);
- </span><span style="color: #008080">49</span> <span style="color: #0000ff">return</span><span style="color: #000000"> cmd.ExecuteScalar();
- </span><span style="color: #008080">50</span> <span style="color: #000000"> }
- </span><span style="color: #008080">51</span> <span style="color: #000000"> }
- </span><span style="color: #008080">52</span>
- <span style="color: #008080">53</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">object</span> ExecuteScalar(<span style="color: #0000ff">string</span> sql, <span style="color: #0000ff">params</span><span style="color: #000000"> SqlParameter[] parameters)
- </span><span style="color: #008080">54</span> <span style="color: #000000"> {
- </span><span style="color: #008080">55</span> <span style="color: #0000ff">using</span> (SqlConnection conn =<span style="color: #000000"> CreateConnection())
- </span><span style="color: #008080">56</span> <span style="color: #000000"> {
- </span><span style="color: #008080">57</span> <span style="color: #0000ff">return</span><span style="color: #000000"> ExecuteScalar(conn, sql, parameters);
- </span><span style="color: #008080">58</span> <span style="color: #000000"> }
- </span><span style="color: #008080">59</span> <span style="color: #000000"> }
- </span><span style="color: #008080">60</span>
- <span style="color: #008080">61</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> DataTable ExecuteQuery(SqlConnection conn, <span style="color: #0000ff">string</span> sql, <span style="color: #0000ff">params</span><span style="color: #000000"> SqlParameter[] parameters)
- </span><span style="color: #008080">62</span> <span style="color: #000000"> {
- </span><span style="color: #008080">63</span> DataTable table = <span style="color: #0000ff">new</span><span style="color: #000000"> DataTable();
- </span><span style="color: #008080">64</span> <span style="color: #0000ff">using</span> (SqlCommand cmd =<span style="color: #000000"> conn.CreateCommand())
- </span><span style="color: #008080">65</span> <span style="color: #000000"> {
- </span><span style="color: #008080">66</span> cmd.CommandText =<span style="color: #000000"> sql;
- </span><span style="color: #008080">67</span> <span style="color: #000000"> cmd.Parameters.AddRange(parameters);
- </span><span style="color: #008080">68</span> <span style="color: #0000ff">using</span> (SqlDataReader reader =<span style="color: #000000"> cmd.ExecuteReader())
- </span><span style="color: #008080">69</span> <span style="color: #000000"> {
- </span><span style="color: #008080">70</span> <span style="color: #000000"> table.Load(reader);
- </span><span style="color: #008080">71</span> <span style="color: #0000ff">return</span><span style="color: #000000"> table;
- </span><span style="color: #008080">72</span> <span style="color: #000000"> }
- </span><span style="color: #008080">73</span> <span style="color: #000000"> }
- </span><span style="color: #008080">74</span>
- <span style="color: #008080">75</span> <span style="color: #000000"> }
- </span><span style="color: #008080">76</span>
- <span style="color: #008080">77</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> DataTable ExecuteQuery(<span style="color: #0000ff">string</span> sql, <span style="color: #0000ff">params</span><span style="color: #000000"> SqlParameter[] parameters)
- </span><span style="color: #008080">78</span> <span style="color: #000000"> {
- </span><span style="color: #008080">79</span> <span style="color: #0000ff">using</span> (SqlConnection conn =<span style="color: #000000"> CreateConnection())
- </span><span style="color: #008080">80</span> <span style="color: #000000"> {
- </span><span style="color: #008080">81</span> <span style="color: #0000ff">return</span><span style="color: #000000"> ExecuteQuery(conn, sql, parameters);
- </span><span style="color: #008080">82</span> <span style="color: #000000"> }
- </span><span style="color: #008080">83</span> <span style="color: #000000"> }
- </span><span style="color: #008080">84</span> <span style="color: #000000"> }
- </span><span style="color: #008080">85</span> }
复习SqlHelper
标签:ble turn log 技术分享 closed reader serve utf8 public