I’m finding a lot of ways to connect to a MS SQL database online. I’m using the connection String in web.config. It’s working perfectly fine for what we want and looks something like this:
<connectionStrings>
<add name="xxx"
connectionString="SERVER=xxx;UID=xxx;Trusted_Connection=Yes;DATABASE=xxx;"
providerName="xxx" />
</connectionStrings>
Is this the best way to connect. I’m then using it with an asp web forms application mostly using gridview
<asp:SqlDataSource ID="MatchDataSource" Runat="server"
SelectCommand="SELECT * FROM [xxx].[Matcxxxh]"
UpdateCommand="UPDATE [xxx].[Matxxxch] SET [xxx] =
@xxx, [xxx] = @xxx, [xxx] =
@xxx WHERE x=@xxxAnd x=@xxx"
ConnectionString="<%$ ConnectionStrings:xxx %>">
<UpdateParameters>
<asp:Parameter Type="String" Name="CSISN"/>
<asp:Parameter Type="String" Name="ProcName"/>
</UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" Runat="server"
DataSourceID="MatchDataSource" Width="100%">
<RowStyle BackColor="white" ForeColor="black" Font-Italic="false" BorderColor="Black" />
<Columns>
<asp:TemplateField SortExpression="xxx" HeaderText="xxx">
<EditItemTemplate>
<asp:TextBox ID="editxxx" Runat="server" Text='<%# Bind("xxx") %>'
MaxLength="15" ToolTip="Enter CSI SN"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" Runat="server" ErrorMessage="You must provide a xxx." ControlToValidate="editxxx">*</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate><asp:Label Runat="server" Text='<%# Bind("CSISN") %>' ID="Label1"></asp:Label></ItemTemplate>
</asp:TemplateField>
My questions are
- Am I going about this correctly?
- Can anyone point me to a proper tutorial which will show best practices?
- How do i close the connection at the end when the script finishes running?
This is one of many ways to use connection strings from configuration. There is nothing wrong with it. Looks fine.
No, as this is a subjective thing and not the kind of thing we answer here.
The connection will close when the script finishes. You don’t need to do anything (
SqlDataSourcetakes care of all the details).