PDA

View Full Version : یه کد خوب برای تست SQL Server 2005



FirstLine
دوشنبه 17 تیر 1387, 16:11 عصر
با سلام
Test SQLServer 2005 in asp.net 3.5
کد زیر برای تست کردن دیتابیس آن لاین هست.
یکیش فایل کد و دیگری فایل aspx اون هست.




<style type="text/css">
.tbl_1
{
text-align: right;
width:400;
}
</style>
<p style="text-align: center">
<asp:Label ID="Label1" runat="server" Text="Test SQL Server 2005"></asp:Label>
</p>
<p style="border: thin dotted #C0C0C0; text-align: center">
<asp:Label ID="lbl_Message" runat="server" ForeColor="#993333"></asp:Label>


<table class="tbl_1" align="center" >
<tr>
<td class="style2" colspan="2">
&nbsp;</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label3" runat="server" Text="Server Name or IP"></asp:Label>
</td>
<td style="text-align: left">
<asp:TextBox ID="txt_Server" runat="server" MaxLength="20"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label4" runat="server" Text="DataBase"></asp:Label>
</td>
<td style="text-align: left">
<asp:TextBox ID="txt_DB" runat="server" MaxLength="20"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label5" runat="server" Text="UserName"></asp:Label>
</td>
<td style="text-align: left">
<asp:TextBox ID="txt_UN" runat="server" MaxLength="20"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>
</td>
<td style="text-align: left">
<asp:TextBox ID="txt_UP" runat="server" MaxLength="20"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2" colspan="2" align=center>
<asp:Button ID="btn_Connect" runat="server" Text="Connect To Server" />
</td>
</tr>
</table>

<asp:GridView ID="DBGridView_Main" runat="server" AllowPaging="True"
AllowSorting="True">
<PagerSettings Position="TopAndBottom" />
</asp:GridView>
</p>
<p style="border: thin dotted #C0C0C0; text-align: center">
<asp:Label ID="Label6" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="lbl_Query" runat="server" Text="Run Query"></asp:Label>
<br />
<asp:TextBox ID="txt_Query" runat="server" Height="100px" TextMode="MultiLine"
Width="400px"></asp:TextBox>
<br />
<asp:Button ID="btn_Query" runat="server" Text="Run Query" />
<br />

<asp:GridView ID="DBGridView_Query" runat="server" AllowPaging="True"
AllowSorting="True">
<PagerSettings Position="TopAndBottom" />
</asp:GridView>
</p>
<p style="border: thin dotted #C0C0C0; text-align: center">
<asp:Label ID="Label8" runat="server" Text="Execute SQL"></asp:Label>
<br />
<asp:Label ID="lbl_Execute" runat="server"></asp:Label>
<br />
<asp:TextBox ID="txt_Execute" runat="server" Height="100px" TextMode="MultiLine"
Width="400px"></asp:TextBox>
<br />
<asp:Button ID="btn_Execute" runat="server" Text="Execute Query" />
</p>
<p style="text-align: center">
&nbsp;</p>







Sub Trim_All()
txt_DB.Text = Trim(txt_DB.Text)
txt_Execute.Text = Trim(txt_Execute.Text)
txt_Query.Text = Trim(txt_Query.Text)
txt_Server.Text = Trim(txt_Server.Text)
txt_UN.Text = Trim(txt_UN.Text)
txt_UP.Text = Trim(txt_UP.Text)
End Sub

Protected Sub btn_Connect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_Connect.Click
Dim obj_DataSource As New System.Web.UI.WebControls.SqlDataSource
Try
Trim_All()
obj_DataSource.ProviderName = "System.Data.SqlClient"

DBGridView_Main.AutoGenerateColumns = True
DBGridView_Main.DataSource = obj_DataSource

'obj_DataSource.ConnectionString = "Data Source=192.168.100.241;Initial Catalog=IranCulture_DB;Persist Security Info=True;User ID=vvv;Password=ccc"
'obj_DataSource.SelectCommand = "SELECT * FROM Public_List"

obj_DataSource.ConnectionString = "Data Source=" + txt_Server.Text + ";Initial Catalog=" + txt_DB.Text + ";Persist Security Info=True;User ID=" + txt_UN.Text + ";Password=" + txt_UP.Text + ""
obj_DataSource.SelectCommand = "SELECT * FROM sysobjects WHERE Type = 'U'"

lbl_Message.Text = "Connect to " + txt_Server.Text + " Success"
DBGridView_Main.DataBind()

Catch ex As Exception
lbl_Message.Text = ex.Message
End Try

' <add name="eghlidConnectionString" connectionString="Data Source=xxxxxxx;Initial Catalog=xxxx;Persist Security Info=True;User ID=xxxx;Password=xxxxxx" providerName="System.Data.SqlClient"/>

End Sub

Protected Sub btn_Query_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_Query.Click
Dim obj_DataSource As New System.Web.UI.WebControls.SqlDataSource
Try
Trim_All()
obj_DataSource.ProviderName = "System.Data.SqlClient"

DBGridView_Query.AutoGenerateColumns = True
DBGridView_Query.DataSource = obj_DataSource

obj_DataSource.ConnectionString = "Data Source=" + txt_Server.Text + ";Initial Catalog=" + txt_DB.Text + ";Persist Security Info=True;User ID=" + txt_UN.Text + ";Password=" + txt_UP.Text + ""
obj_DataSource.SelectCommand = txt_Query.Text

lbl_Query.Text = "Run Query OK"
DBGridView_Query.DataBind()

Catch ex As Exception
lbl_Message.Text = ex.Message
End Try
End Sub

Protected Sub btn_Execute_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_Execute.Click
Dim obj_DataSource As New System.Web.UI.WebControls.SqlDataSource
Try
Trim_All()
obj_DataSource.ProviderName = "System.Data.SqlClient"

DBGridView_Main.AutoGenerateColumns = True
DBGridView_Main.DataSource = obj_DataSource

obj_DataSource.ConnectionString = "Data Source=" + txt_Server.Text + ";Initial Catalog=" + txt_DB.Text + ";Persist Security Info=True;User ID=" + txt_UN.Text + ";Password=" + txt_UP.Text + ""
obj_DataSource.SelectCommand = txt_Execute.Text

lbl_Execute.Text = "Run Query OK"
DBGridView_Main.DataBind()

Catch ex As Exception
lbl_Message.Text = ex.Message
End Try
End Sub




با تشکر