Click here to Skip to main content
15,909,466 members
Home / Discussions / Database
   

Database

 
AnswerRe: alternative to rewire CASE WHEN in query Pin
PIEBALDconsult26-Jul-10 8:05
mvePIEBALDconsult26-Jul-10 8:05 
AnswerRe: alternative to rewire CASE WHEN in query Pin
Jörgen Andersson27-Jul-10 21:31
professionalJörgen Andersson27-Jul-10 21:31 
QuestionMessage Removed Pin
25-Jul-10 1:24
bapu288925-Jul-10 1:24 
AnswerRe: SQL INSERT ISSUE Pin
Matt U.25-Jul-10 6:23
Matt U.25-Jul-10 6:23 
AnswerRe: SQL INSERT ISSUE Pin
Stoffy197225-Jul-10 7:13
Stoffy197225-Jul-10 7:13 
QuestionRe: SQL INSERT ISSUE Pin
bapu288925-Jul-10 21:15
bapu288925-Jul-10 21:15 
AnswerRe: SQL INSERT ISSUE Pin
PIEBALDconsult26-Jul-10 8:24
mvePIEBALDconsult26-Jul-10 8:24 
AnswerRe: SQL INSERT ISSUE Pin
Stoffy197226-Jul-10 8:35
Stoffy197226-Jul-10 8:35 
The first stored procedure:
CREATE PROCEDURE [dbo].[InsertLocation]
(
	@City nvarchar(50),
	@State nvarchar(50),
	@LocationID int out
)
AS
	INSERT INTO [Location] (City,State)
		VALUES (@City, @State)

	SELECT @LocationID = SCOPE_IDENTITY()
GO


and the second:
CREATE PROCEDURE [dbo].[InsertUserData]
(
	@LocationID int,
	@FirstName nvarchar(25),
	@LastName nvarchar(25)
)
AS
	SET NOCOUNT ON;

	INSERT INTO [UserData] (LocationID, FirstName, LastName)
		(SELECT @LocationID, @FirstName, @LastName)
GO


My code, in c#:

Assuming that the array of names is filled with items
private static void insertData()
{
    try
    {
        //Create connection string using SqlConnectionStringBuilder
        SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder();
        sb.DataSource = ".\\SQLEXPRESS";
        sb.InitialCatalog = "Database";
        sb.IntegratedSecurity = true;

        // Create SqlConnection
        using (SqlConnection cn = new SqlConnection(sb.ToString()))
        {
            // Open the connection
            cn.Open();

            // Create command to insert location
            SqlCommand insertLocationCommand = new SqlCommand("InsertLocation", cn);
            insertLocationCommand.CommandType = CommandType.StoredProcedure;
            // Add first parameter for City (nvarchar)
            insertLocationCommand.Parameters.Add("@City", SqlDbType.NVarChar).Value = "City1";
            // Add second parameter for State (nvarchar)
            insertLocationCommand.Parameters.Add("@State", SqlDbType.NVarChar).Value = "State1";
            // Add third parameter for LocationID (int)...
            insertLocationCommand.Parameters.Add("@LocationID", SqlDbType.Int).Value = 0;
            // ... and mark it as OUTPUT-Parameter
            insertLocationCommand.Parameters["@LocationID"].Direction = ParameterDirection.Output;
            // Execute command...
            insertLocationCommand.ExecuteNonQuery();
            // ... and get value of out parameter
            int locationID = (int)insertLocationCommand.Parameters["@LocationID"].Value;

            // Create command to insert user data
            SqlCommand insertUserDataCommand = new SqlCommand("InsertUserData", cn);
            insertUserDataCommand.CommandType = CommandType.StoredProcedure;
            // Add first parameter with new location id, add value (locationID)
            insertUserDataCommand.Parameters.Add("@LocationID", SqlDbType.Int).Value = locationID;
            // Add second parameter for FirstName (nvarchar), add value later in loop!
            insertUserDataCommand.Parameters.Add("@FirstName", SqlDbType.NVarChar);
            // Add third parameter for LastName (nvarchar), add value later in loop!
            insertUserDataCommand.Parameters.Add("@LastName", SqlDbType.NVarChar);
            for (int i = 0; i < FirstNameArray.Count; i++)
            {
                // set new values
                insertUserDataCommand.Parameters["@FirstName"].Value = FirstNameArray[i].ToString();
                insertUserDataCommand.Parameters["@LastName"].Value = LastNameArray[i].ToString();

                // Execute command...
                insertUserDataCommand.ExecuteNonQuery();
            }

            Console.WriteLine("Data Saved Successfully !");
        }
    }
    catch (SqlException ex)
    {
        Console.WriteLine(ex.ToString());
    }
    finally
    {
        FirstNameArray.Clear();
        LastNameArray.Clear();
    }
}


If you can't understand the code in c# let me know, i'll try to convert it but i am not able to test it...
GeneralRe: SQL INSERT ISSUE Pin
bapu288926-Jul-10 21:23
bapu288926-Jul-10 21:23 
AnswerHow to ask a question Pin
Luc Pattyn26-Jul-10 23:34
sitebuilderLuc Pattyn26-Jul-10 23:34 
QuestionInstall SQL 2008 r2 express with my application Pin
bonzaiholding24-Jul-10 22:29
bonzaiholding24-Jul-10 22:29 
AnswerRe: Install SQL 2008 r2 express with my application Repost Pin
Richard MacCutchan25-Jul-10 1:40
mveRichard MacCutchan25-Jul-10 1:40 
Answercross-post Pin
Luc Pattyn25-Jul-10 2:18
sitebuilderLuc Pattyn25-Jul-10 2:18 
Question[MS SQL SERVER] Create constraints in a cyclic relations Pin
Lutosław23-Jul-10 23:32
Lutosław23-Jul-10 23:32 
Questionanother query Pin
reza assar23-Jul-10 21:40
reza assar23-Jul-10 21:40 
AnswerRe: another query Pin
Goutam Patra23-Jul-10 22:58
professionalGoutam Patra23-Jul-10 22:58 
GeneralRe: another query Pin
reza assar24-Jul-10 1:03
reza assar24-Jul-10 1:03 
Questionquery Pin
reza assar23-Jul-10 21:32
reza assar23-Jul-10 21:32 
AnswerRe: query Pin
Goutam Patra23-Jul-10 22:57
professionalGoutam Patra23-Jul-10 22:57 
AnswerRe: query Pin
Mycroft Holmes23-Jul-10 23:05
professionalMycroft Holmes23-Jul-10 23:05 
QuestionMessage Removed Pin
23-Jul-10 21:06
prachidalwadi23-Jul-10 21:06 
AnswerRe: How to optimize the query with more than one left outer join Pin
CitrusTech23-Jul-10 21:14
CitrusTech23-Jul-10 21:14 
Questionsql problem Pin
bapu288923-Jul-10 20:40
bapu288923-Jul-10 20:40 
AnswerRe: sql problem Pin
Goutam Patra23-Jul-10 21:31
professionalGoutam Patra23-Jul-10 21:31 
QuestionRe: sql problem Pin
bapu288923-Jul-10 23:33
bapu288923-Jul-10 23:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.