C#. NET Database Operation Memo

Source: Internet
Author: User
Tags anonymous count reference tostring
Data | Database one. Database connection
The attached database information is available in the following file for easy unified management.
web.config//still don't understand this. Better learn, I think very useful,. NET is of course to use. NET things.

<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<configSections>
<sectiongroup name= "System.Web" >
<section name= "Database" type= "system.configuration.namevaluesectionhandler,system,version=1.0.5000.0,culture =neutral,publickeytoken=b77a5c561934e089 "/>
</sectionGroup>
</configSections>


<system.web>

<!--ダイナミックデバッグコンパイル
Aspxデバッグを has demonstration にするには, compilation debug= "true" I set します. この をfalseに Setting すると,

このアプリケーションの Aimi line のパフォーマンスが up します.

デバッグシンボル (. pdb intelligence) をコンパイルされたページに 挿 into するには,

Compilation debug= "True" I setting します. この set によって, Aimi line speed 遅 o Muhammad to きなファイルが.

デバッグ time にのみ, この をtrueに set して,

それ outside のときは, often にfalseに set してください. Detailed については,

ASP. Netファイルのデバッグドキュメントを reference してください.

-->
<compilation
Defaultlanguage= "C #"
Debug= "true"
>
<assemblies>
</assemblies>
</compilation>


<!--カスタムエラーメッセージの setting

カスタムエラーメッセージを has demonstration にするにはcustomerrors mode= "on" または "RemoteOnly" にします. Demonstration にする は "Off" I set します.

Backup する each エラーに<error>タグを append してください.
defaultredirect= "Err.htm"

"オン" often にカスタム (understanding しやすい) メッセージを represents します.

"オフ" Often I detailed なasp.netエラー reports を said します.

"リモートのみ" ローカルwebサーバーで Aimi line していないユーザーに impose してのみ,

Often にカスタム (understanding しやすい) メッセージを represents します. この set は, セキュリティを to consider して, アプリケーションの detailed がリモートのクライアントに
Represents されないようにする push 奨 された setting です.

-->
<customerrors mode= "RemoteOnly" defaultredirect= "/main/defaulterror.aspx" >
<error statuscode= "404" redirect= "/main/error500.htm"/>
<error statuscode= "redirect="/main/error500.htm "/>"

</customErrors>



<!--the "'" ' ' ' ' ' ' ' ' ' ', ' ' ' ' ' ' ' ' ' ' ' "" "" "" "" "" "" "" "" "" "" "" "" "" "
このセクションは, アプリケーションの to confirm ポリシーを set します. Using できるモードは, Windows,

"Forms", "Passport" および "None" です.


"None" が Aimi line されません.
"Windows" Iisはアプリケーションの I 従 って (Basic, ダイジェスト, または system Windows)
を Aimi Line します. Anonymous アクセスはiisでは demonstration でなければなりません.

The Forms ユーザーが を するカスタムフォーム (webページ) を offers してください.

そして, アプリケーションで recognized してください. ユーザーの Information のトークンはcookieに Save されています.

"Passport" confirmation は, p a のログオンおよびメンバサイトのコアプロファイルサービスを provide する
Microsoftにより provides される management certification サービスを use して Aimi line されます.

-->
<identity impersonate= "true"/>
<authentication mode= "Windows"/>
<!--権 Limit setting

このセクションは, アプリケーションの 権 limit のポリシーを set します. この set により, ユーザーまたはロールによるアプリケーションリソースへのアクセスを can したり,

Refuse to したりできます. ワイルドカード: "*" は Full member を, "?" は Anonymous
(not recognized) ユーザーを table します.

-->

<authorization>
<allow users= "*"/> <!--* All ユーザーへの can-->
<!--<allow users= "[ユーザーのコンマ area I did easily a 覧]"
Roles= "[ロールのコンマ District I did easily one 覧]"/>
<deny users= "[ユーザーのコンマ District Cut I did easily one 覧]"
Roles= "[ロールのコンマ District I did easily one 覧]"/>
-->
</authorization>

<!--アプリケーションレベルのトレースログの setting

アプリケーションレベルのトレースは, アプリケーション the full ページのトレースログ output を demonstration にします.

アプリケーショントレースログを has demonstration にするには, trace enabled= "true" I set します. Pageoutput= "true" I set された field,

トレース sentiment report が each ページの under I said されます. それ outside the は,

webアプリケーションルートから "Trace.axd" ページを reference してアプリケーショントレースを

Represents できます.

-->
<trace
Enabled= "false"
requestlimit= "10"
Pageoutput= "false"
Tracemode= "SortByTime"
Localonly= "true"
/>

<!--セッション form

Established では, asp.netはcookieを use して, requirements がどのセッションに するかを knowledge します.

Cookieが uses できない field-は, urlにセッション を and することで.

Cookieを has demonstration にするには, sessionstateをcookieless= "false" I set してください.

-->
<sessionstate
Mode= "InProc"
Stateconnectionstring= "tcpip=127.0.0.1:42424"
sqlconnectionstring= "Data source=127.0.0.1; Trusted_connection=yes "
Cookieless= "false"
timeout= "720"
/>

<!--グローバリゼーション
shift_jisこのセクションはアプリケーションのグローバリゼーション set を line います.
Utf-8-->
<globalization
Requestencoding= "Utf-8"
Responseencoding= "Utf-8"
/>

<database>
<!--add key= "Portaldb" value= "server= (local) \netsdk;database=portal;integrated Security=sspi"/-->
<add key= "strconn" value= "Server=107.12.165.199;database=nwind;uid=nwind;pwd=nwind"/>
</database>

</system.web>
<appSettings>
<add key= "Nwind" value= "Server=107.12.165.199;database=nwind;uid=nwind;pwd=nwind"/>
</appSettings>

</configuration>

Connection code:
Using System.Data.SqlClient;
The namespace required for using system.collections.specialized;//to manipulate XML
String strconn= (String) (NameValueCollection) context.getconfig ("System.web/database")) ["strconn"];

Operation Code:
1, through the database stored procedures to operate the database, through the middle layer to do the project is the most ideal. can improve security and overall performance.
using (SqlConnection conn = new SqlConnection (strconn))
{
SqlCommand cmd = new SqlCommand ("SP_SP", conn);
cmd.commandtype=commandtype.storedprocedure;//declaration is a call to a stored procedure

Cmd. Parameters.Add ("@SNO", sqldbtype.varchar,9);
Cmd. Parameters.Add ("@TD", sqldbtype.smalldatetime);
Cmd. Parameters.Add ("@S_NOTE", sqldbtype.varchar,100);//Add variable parameter

Cmd. parameters["@SNO"]. Value=sno. Text;
Cmd. parameters["@TD"]. Value=checkbase.text;
Cmd. parameters["@S_NOTE"]. value=snote.text;//to the parameter value

Conn. Open ();
if (cmd. ExecuteNonQuery ()!=0)//execution does not return a collection
okmsg.text= "Sdts Deng registration Complete";
Else
okmsg.text= "Sdts Deng Registration failure";
}


2, direct output SQL statement

String strsql= "UPDATE NWind.NUser.DATA SET sno= '" +sno. Text;//sql server needs to indicate the name of the database in which the table resides and the user name that created the table
if (checkbase.text!= "")
strsql+= "', MONTH = '" +checkbase.text+ "/01";
strsql+= "' WHERE SNO = '" +sno. text+ "'";
Conn. Open ();
SqlCommand cmd = new SqlCommand (strsql,conn);
if (cmd. ExecuteNonQuery ()!=0)
okmsg.text= "データが Deng registration しました";
Else
okmsg.text= "Sdts Deng Registration failure";
}
3. Retrieval syntax SqlDataAdapter is used in conjunction with datasets and is generally used for special operations on access to datasets
More commonly used

Private DataSet GetData ()
{
String strconn= (String) (NameValueCollection) context.getconfig ("System.web/database")) ["strconn"];
using (SqlConnection conn = new SqlConnection (strconn))
{
SqlCommand cmd = new SqlCommand ("Sp_co", conn);
Cmd.commandtype=commandtype.storedprocedure;
Cmd. Parameters.Add ("@place", sqldbtype.varchar,2);
Cmd. parameters["@place"]. Value=place.text;
Conn. Open ();

SqlDataAdapter da = new SqlDataAdapter ();
Da. Selectcommand=cmd;
DataSet ds=new DataSet ();
Da. Fill (ds, "VCO");
You can use a dataset like a local database, where you can store multiple datasheets and implement the functionality you want to achieve. Details such as table processing have column deletions increased, and multiple tables are processed. Wait a minute
count.text= "ヒット:" +ds. tables["VCO"]. Rows.Count.ToString () + "pieces";
return DS;
}

}

4, when only used to read the data recommended to use SqlDataReader, read faster than the above a lot faster
private void Dataloader ()
{
String strconn= (String) (NameValueCollection) context.getconfig ("System.web/database")) ["strconn"];
using (SqlConnection conn = new SqlConnection (strconn))
{
SqlCommand cmd = new SqlCommand ("Sp_list", conn);
Cmd.commandtype=commandtype.storedprocedure;
Cmd. Parameters.Add ("@no", sqldbtype.varchar,6);
Cmd. parameters["@no"]. Value=no. Text;
Conn. Open ();
using (SqlDataReader dr = cmd. ExecuteReader ())
{
if (Dr. Read ())
{
c.text=dr["C"]. ToString (); Get data from a column name
m.text=dr["MTC"]. ToString (); //
}
Else
Okmsg.text= "このデータがありません, and は 検 sent I did easily first of the は violation of law です. ";
}

}

}
Note that all of these are using a using (), and the advantage is that you do not need to connect the open database to the close () operation.
Because I have a very poor memory, so I like this method

Let's get here first. What the stored procedures in the database do specifically. Let's talk about it later.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.