Sqlserver
 sql >> डेटाबेस >  >> RDS >> Sqlserver

सी # से SQL सर्वर 2008 में डेटाटेबल पास करना

आप गायब थे a.TypeName ="dbo.TableTypeInitial"; इस कथन को "a.SqlDbType =SqlDbType.Structured;" से पहले रखें।

उपयोग करें

cmd.CommandText = "EXEC FinishRegisterChoiceUserInitial @UserId, @TableTypeInitial "; 

के बजाय

cmd.CommandText = "EXEC FinishRegisterChoiceUserInitial @UserId, @CurrentTableInitial "; 

एसक्यूएल सर्वर स्क्रिप्ट:

         CREATE TABLE [Target]
         (
         [ID] [int] NOT NULL PRIMARY KEY IDENTITY,
         [FirstName] [varchar](100)NOT NULL,
         [LastName] [varchar](100)NOT NULL,
         [Email] [varchar](200) NOT NULL
         )
        CREATE TYPE [TargetUDT] AS TABLE
         (
         [FirstName] [varchar](100)NOT NULL,
         [LastName] [varchar](100)NOT NULL,
         [Email] [varchar](200) NOT NULL
         )
         CREATE PROCEDURE AddToTarget(@TargetUDT TargetUDT READONLY)
         AS
         BEGIN
               INSERT INTO [Target]
               SELECT * FROM @TargetUDT
         END

नमूना कोड:

public static void StartProcess()
        {
            //Create a local data table to hold customer records
            DataTable dtCustomers = new DataTable("Customers");
            DataColumn dcFirstName = new DataColumn("FirstName", typeof(string));
            DataColumn dcLastName = new DataColumn("LastName", typeof(string));
            DataColumn dcEmail = new DataColumn("Email", typeof(string));
            dtCustomers.Columns.Add(dcFirstName);
            dtCustomers.Columns.Add(dcLastName);
            dtCustomers.Columns.Add(dcEmail);
            //Add customer 1
            DataRow drCustomer = dtCustomers.NewRow();
            drCustomer["FirstName"] = "AAA";
            drCustomer["LastName"] = "XYZ";
            drCustomer["Email"] = "[email protected]";
            dtCustomers.Rows.Add(drCustomer);
            //Add customer 2
            drCustomer = dtCustomers.NewRow();
            drCustomer["FirstName"] = "BBB";
            drCustomer["LastName"] = "XYZ";
            drCustomer["Email"] = "[email protected]";
            dtCustomers.Rows.Add(drCustomer);
            //Add customer 3
            drCustomer = dtCustomers.NewRow();
            drCustomer["FirstName"] = "CCC";
            drCustomer["LastName"] = "XYZ";
            drCustomer["Email"] = "[email protected]";
            dtCustomers.Rows.Add(drCustomer);
            //Create Connection object to connect to server/database
            SqlConnection conn = new SqlConnection(ConStr);
            conn.Open();
            //Create a command object that calls the stored procedure
            SqlCommand cmdCustomer = new SqlCommand("AddToTarget", conn);
            cmdCustomer.CommandType = CommandType.StoredProcedure;
            //Create a parameter using the new SQL DB type viz. Structured to pass as table value parameter
            SqlParameter paramCustomer = cmdCustomer.Parameters.Add("@TargetUDT", SqlDbType.Structured);
            paramCustomer.Value = dtCustomers;
            //Execute the query
            cmdCustomer.ExecuteNonQuery();        
        }



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. SQL सर्वर में टेबल-वैल्यूड फंक्शन बनाएं

  2. SQL सर्वर (T-SQL) से ईमेल कैसे भेजें

  3. SQL सर्वर में सोमवार को सप्ताह के पहले दिन के रूप में कैसे सेट करें?

  4. टीएसक्यूएल राउंड अप दशमलव संख्या

  5. SQL सर्वर प्रतिकृति समस्या निवारण