Posted by Brad | Posted in SQL Server | Posted on 10-12-2012
So you want to update a single row in your table and then return the row identifier that was affected, here’s one way of going about it. Remember SCOPE_IDENTITY() is about inserting so as we are updating here we need a little something different.
This will update a single record and output the id of the record it updated
DECLARE @TmpTable TABLE ( primary_id INT )
UPDATE dbo. table
SET someColumn= ‘Some value’
OUTPUT INSERTED. primary_id INTO @TmpTable
WHERE primary_id = (SELECT MIN( primary_id )
FROM dbo. table nolock WHERE someColumn <> ’Some value’ )
SELECT * FROM @TmpTable
Posted by Brad | Posted in Recommendations, SQL Server | Posted on 11-10-2011
Today I found out something that I found out years ago, if you like me need to use multiple SQL servers but can’t remember things by IP address or would prefer not having to type login information every time you want to connect then use registered servers.
Registered servers allow you to easily check if a server is running, create groups of servers, use user-friendly names for your servers, and import/export your registered server groups.
To view and add a registered server, start up SQL Server Management Studio go to View > Registered Servers. Then in the Registered Servers pane, right-click Local Server Groups and click New Server Registration. Alternatively if you already have them open right now you can just right click the instance and select register then enter your friendly name… super easy!

Then just enter the server name, select the authentication, key in the User name and Password if necessary, and check Remember password.
Now each time you start up SQL Server Management Studio, your registered servers will be viewable in the Registered Servers pane. Simply double-click the server you wish to navigate and that server will be opened within the Object Explorer.
Now I need to find out how to make this the default view when opening SSMS..
Posted by Brad | Posted in C# | Posted on 13-07-2010
Something simple, you want to use ADO.NET to bind data from SQL Server to a GridView, here is your code behind
[codesyntax lang="csharp"]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string conn = ConfigurationManager.ConnectionStrings["STRINGNAME"].ConnectionString;
SqlConnection con = new SqlConnection(conn);
using (con)
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Table", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
gv.DataSource = rdr;
gv.DataBind();
}
}
}
[/codesyntax]
Here is your control
[codesyntax lang="csharp"]
<asp:GridView ID=”gv” runat=”server” />
[/codesyntax]
Posted by Brad | Posted in C#, Ramblings | Posted on 23-06-2009
These are the minimum using directives to add at the top of the code behind for building an HTML table from a SQL Server to a SqlDataReader concisely.
[codesyntax lang="csharp"]
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;
[/codesyntax]
Your table in the aspx file can be as simple as
[codesyntax lang="html4strict"]
<table id="Products" runat="server"></table>
[/codesyntax]
And the code behind that does the work, taking the value from a dropdown list control called Category, and retrieving data from Northwind is
[codesyntax lang="csharp"]
using(SqlConnection cnn = new SqlConnection("Data Source=YOURSERVER; Database=Northwind; UID=YOURDBUSERID; PWd=YOURDBPASSWORD"))
{
SqlCommand cmd;
SqlDataReader rdr;
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell("th");
cell.InnerText = "Product ID";
row.Cells.Add(cell);
cell = new HtmlTableCell("th");
cell.InnerText = "Product Name";
row.Cells.Add(cell);
cell = new HtmlTableCell("th");
cell.InnerText = "Units in stock";
row.Cells.Add(cell);
Products.Rows.Add(row);
cmd = new SqlCommand("SELECT ProductID, ProductName, UnitsInStock FROM Products WHERE CategoryID = "
+ Category.SelectedValue, cnn);
cnn.Open();
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
row = new HtmlTableRow();
cell = new HtmlTableCell();
cell.InnerText = rdr["ProductID"].ToString();
row.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = rdr["ProductName"].ToString();
row.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = rdr["UnitsInStock"].ToString();
row.Cells.Add(cell);
Products.Rows.Add(row);
}
}
[/codesyntax]