2012-01-07 5 views
0

WebMethodスクリプトを自分の.aspxページに入れてデータベースに接続します。しかし、私は "using System.Data"と "using System.SqlClient"をどこかに置いてADO.NETを使う必要があるようです。 .aspxページ(not.aspx.csページ)でWebMethodを使用してデータベースに接続することはできますか?そうであれば、どうすれば使用できますか?要件はaspx.csページを.aspxページに入れていないためになぜデータベースに接続する.aspxページ内のWebMethod

<script runat="server"> 

[System.Web.Services.WebMethod] 
public static void send() 
{ 
    SqlConnection con = new SqlConnection("Data Source=.\\SQLExpress;Initial Catalog=Hello;Integrated Security=True"); 
    SqlCommand cmd = new SqlCommand(queryString, con); 
    using (con) 
    { 
     con.Open(); 
     cmd.ExecuteNonQuery(); 
    } 
} 

</script> 
+0

を試すことができますか? – justinlabenne

+0

私はWebMethodを使用するためにこれを行いました。 .asmxページまたは.aspxページ以外のページでWebMethodを使用することはできますか? –

+0

page.aspx.cs(コードビハインド)に入れることができます。 "using System.Data;"を追加する必要があります。と "using System.Data.SqlClient;"頂点で。 – justinlabenne

答えて

0

uが

<script runat="server"> 
[System.Web.Services.WebMethod] 
public static void send() 
{ 
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("Data Source=.\\SQLExpress;Initial Catalog=Hello;Integrated Security=True"); 
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(queryString, con); 
using (con) 
{ 
    con.Open(); 
    cmd.ExecuteNonQuery(); 
} 
} 

</script> 
関連する問題