Thursday 16 June 2016

SQL SERVER – Passing multiple values through one parameter in a stored procedure



In this article, I developed a solution How to use multiple values for IN clause using same parameter (SQL Server). Recently, I received a query, inquiring how to pass multiple values through one parameter in a stored procedure.

Given below the Methods :

In this solution, you need to pass a single comma delimiter string to the stored procedure. Once it is passed, you need to replace the parameter with single quote and create a dynamic query and execute it.
Given below is the Example Code and Script.



SCRIPT


set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

Create PROCEDURE [dbo].[Get_StockHistory]
(@Id varchar(500))
AS
Begin
set @Id = Replace(@Id, ',',''',''')

DECLARE @SqlQuery VARCHAR(MAX)
SET @ SqlQuery = 'SELECT Column1, Column2, Column3 FROM TabelName
            WHERE Column1 IN (''' + @Id + ''') ORDER BY ItemId'

EXEC (@SqlQuery)
END


CODE

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

Create PROCEDURE [dbo].[Get_StockHistory]
(@ItemId varchar(500))
AS
Begin
declare @Itid
set @Itid='338,274,275,289,58,1227,322,323,324,325'
SET @Itid = REPLACE(@id,',',''',''')

DECLARE @SqlQuery VARCHAR(MAX)
SET @ SqlQuery = 'SELECT ItemId,ItemName,Price,AvailableQty FROM ItemDetail
            WHERE ItemId IN (''' + @Itid + ''') ORDER BY ItemId'
EXEC (@SqlQuery)
END

Import records from Excel to DataTable



In this article i am going to explain how to import records from excel sheet to DataTable Using C# dot net.

Design code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default1.aspx.cs" 
 Inherits="CSV" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
<asp:FileUpload ID="lImport" runat="server" />
<asp:Button ID="btnImport" runat="server" Text="Import" OnClick="btnImport_Click"/>
</div> </form></body></html>
  
We have to use the following namespaces.

using System;
using System.Data;
using System.Data.OleDb;


C# Code

protected void btnIpload_Click(object sender, EventArgs e)
    {
        ImportExcelToDatatable();
    }

    void ImportExcelToDatatable()
    {
        try
        {
            if (lImport.HasFile)
            {
                string FileName = lImport.FileName;
                string path = string.Concat(Server.MapPath("~/Document/" + lImport.FileName));

                OleDbConnection OleDbcon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;");
                OleDbCommand command = new OleDbCommand("SELECT * FROM [Employee$]", OleDbcon);
                OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(command);

                OleDbcon.Open();
                OleDbDataAdapter da = new OleDbDataAdapter(command);
                DataSet ds = new DataSet();
                da.Fill(ds);
                DataTable dt = ds.Tables[0];
            }
        }
        catch (Exception ex)
        {
          
        }   
    }