<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WinSoft.se &#187; Data access</title>
	<atom:link href="http://www.winsoft.se/tag/data-access/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.winsoft.se</link>
	<description>Development with focus on Visual Basic .NET</description>
	<lastBuildDate>Thu, 26 Jan 2012 19:28:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Inserting a value in an identity column</title>
		<link>http://www.winsoft.se/2010/05/inserting-a-value-in-an-identity-column/</link>
		<comments>http://www.winsoft.se/2010/05/inserting-a-value-in-an-identity-column/#comments</comments>
		<pubDate>Tue, 25 May 2010 15:12:11 +0000</pubDate>
		<dc:creator>Anders Hesselbom</dc:creator>
				<category><![CDATA[Visual Basic 9]]></category>
		<category><![CDATA[Data access]]></category>

		<guid isPermaLink="false">http://www.winsoft.se/?p=1148</guid>
		<description><![CDATA[In SQL Server 2008, when a column is set to auto increment (Identity Specification), you are normally not allowed to give that column a value. If you are copying data from one database to another in Visual Basic, perhaps by loading a DataSet object using one connection, and inserting it&#8217;s data using another, this can [...]]]></description>
			<content:encoded><![CDATA[<p>In SQL Server 2008, when a column is set to auto increment (Identity Specification), you are normally not allowed to give that column a value. If you are copying data from one database to another in Visual Basic, perhaps by loading a DataSet object using one connection, and inserting it&#8217;s data using another, this can be a problem.</p>
<p>The table option you must set, to be allowed to insert a value in an identity column is called <strong>IDENTITY_INSERT</strong>. So add this line to your command object to make it work:</p>
<pre>SET IDENTITY_INSERT dbo.Products ON;</pre>
<p>The following <strong>INSERT</strong> statement can give a value to the identity column, if the value complies with the rules for that column. The line of code could look something like this:</p>
<pre>Using X As New SqlClient.SqlCommand( _
    "SET IDENTITY_INSERT dbo.Products ON; INSERT...</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.winsoft.se/2010/05/inserting-a-value-in-an-identity-column/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting the meta data for a table from Visual Basic</title>
		<link>http://www.winsoft.se/2010/01/getting-the-meta-data-for-a-table-from-visual-basic/</link>
		<comments>http://www.winsoft.se/2010/01/getting-the-meta-data-for-a-table-from-visual-basic/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 18:48:16 +0000</pubDate>
		<dc:creator>Anders Hesselbom</dc:creator>
				<category><![CDATA[Visual Studio 10]]></category>
		<category><![CDATA[Data access]]></category>

		<guid isPermaLink="false">http://www.winsoft.se/?p=850</guid>
		<description><![CDATA[The last time I did this, I needed to build a custom Visual Basic entity generator. After two hours of coding, I had a tiny Windows Forms application that took some connection information and a table name, and returned an entity class with some properties and methods, and a collection class. With initialization, database write-back [...]]]></description>
			<content:encoded><![CDATA[<p>The last time I did this, I needed to build a custom Visual Basic entity generator. After two hours of coding, I had a tiny Windows Forms application that took some connection information and a table name, and returned an entity class with some properties and methods, and a collection class. With initialization, database write-back and all.</p>
<p>From the Management Studio, a call to the <strong>sp_help</strong> procedure and pass the name of the table you want to receive meta data about, like this:</p>
<pre>EXEC sp_help 'dbo.spt_values'</pre>
<p>(I still haven&#8217;t created any databases on the computer that I am writing this from, so I am grabbing the meta data from the <strong>spt_values</strong> table in the master database.</p>
<p>The procedure returns a few sets, and the second one contains a list of the table columns.</p>
<p>To call this from Visual Basic, you should know that the procedure is located in the <strong>sys</strong> namespace, and the parameter that it expects is called <strong>@objname</strong>. So, if you are using a dataset, the table with index 1 contains the column information. If you are using a data reader, you can call the <strong>NextResult</strong> function, like so (in a console application):</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">Using Cn <span class="kwrd">As</span> <span class="kwrd">New</span> SqlClient.SqlConnection(<span class="str">"Data Source=.;Initial Catalog=master;Integrated Security=True"</span>)
    Cn.Open()
    Using Cmd <span class="kwrd">As</span> <span class="kwrd">New</span> SqlClient.SqlCommand(<span class="str">"[sys].[sp_help]"</span>, Cn)
        Cmd.CommandType = CommandType.StoredProcedure
        Cmd.Parameters.AddWithValue(<span class="str">"@objname"</span>, <span class="str">"dbo.spt_values"</span>)
        <span class="kwrd">Dim</span> R <span class="kwrd">As</span> SqlClient.SqlDataReader = Cmd.ExecuteReader()
        R.NextResult()
        <span class="kwrd">Dim</span> ColumnNameColumn <span class="kwrd">As</span> <span class="kwrd">Integer</span> = R.GetOrdinal(<span class="str">"Column_name"</span>)
        <span class="kwrd">Dim</span> ColumnTypeColumn <span class="kwrd">As</span> <span class="kwrd">Integer</span> = R.GetOrdinal(<span class="str">"Type"</span>)
        <span class="kwrd">Dim</span> ColumnLength <span class="kwrd">As</span> <span class="kwrd">Integer</span> = R.GetOrdinal(<span class="str">"Length"</span>)
        <span class="kwrd">While</span> R.Read()
            Console.WriteLine(R.GetString(ColumnNameColumn))
            Console.WriteLine(R.GetString(ColumnTypeColumn))
            Console.WriteLine(R.GetInt32(ColumnLength).ToString())
        <span class="kwrd">End</span> <span class="kwrd">While</span>
        R.Close()
    <span class="kwrd">End</span> Using
    Cn.Close()
<span class="kwrd">End</span> Using</pre>
<p>If you are using this to create something like a code generator, remember that <strong>Length</strong> column holds the column size in bytes. This means that a nvarchar (Unicode string columns) with the length set to 10, only can hold 5 characters.</p>
<p>The above code targets .NET Framework 3.5, but it would look exactly the same in the version above and below. It is written in Visual Basic 10 (VBx).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.winsoft.se/2010/01/getting-the-meta-data-for-a-table-from-visual-basic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

