在插入之前从数据集中剪切字符

时间:2016-07-25 14:44:52

标签: c# sql dataset sqlbulkcopy

我正在运行一个sql查询,它将结果存储在数据集中,然后如果数据集有行,它将运行一个查询以插入到表中。问题是出来的数据是字符串并保存为001234,而我希望数据作为1234存储在我的表中,因为它作为主键。我曾经想过尝试用数据集和子字符串中的每一行来做,但我收到一个错误说: -

数据源中String类型的给定值无法转换为指定目标列的int类型。
我试图将它作为int存储在数据库表中。

        //Runs rollID query and stores in dataset and datatable
    public DataSet GetDataSet(string sqlCommand, string ConnectionString)
    {
        string connectionString = (ConfigurationManager.ConnectionStrings["datConnectionString"].ConnectionString);
        DataSet ds = new DataSet();
        using (SqlCommand cmd = new SqlCommand(
            sqlCommand, new SqlConnection(connectionString)))
        {
            cmd.Connection.Open();
            DataTable rollTable = new DataTable();
            rollTable.Load(cmd.ExecuteReader());
            ds.Tables.Add(rollTable);

            if (rollTable.Rows.Count > 0)
            {
                foreach (DataRow rw in rollTable.Rows)
                {
                    //Get StartTime in Time format
                    string StaffID = rw["staff_code"].ToString();

                    if (string.IsNullOrEmpty(StaffID) == true)
                    {
                        //Do nothing
                    }
                    else
                    {
                        string ShortStaffID = StaffID.Substring(2);
                        rw["staff_code"] = ShortStaffID.ToString();
                    }

                }

                //Gets data from datatable and inserts it into table within database 
                string consString = ConfigurationManager.ConnectionStrings["rollPlusConnectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(consString))
                {
                    using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
                    {

                        //Set the database table name
                        sqlBulkCopy.DestinationTableName = "dbo.roll";

                        if (rollTable.Rows.Count > 0)
                        {
                            con.Open();
                            sqlBulkCopy.WriteToServer(rollTable);
                            con.Close();
                        }
                        else
                        {
                        }
                        return ds;
                    }

                }
            }
        }

    }

1 个答案:

答案 0 :(得分:1)

HttpComponentsMessageSender messageSender = new HttpComponentsMessageSender (); AuthScope authscope; NTCredentials credentials; CredentialsProvider credentialsProvider; Registry<AuthSchemeProvider> registry; RequestConfig requestConfig; authscope = new AuthScope (HOST_IP, HOST_PORT); credentials = new NTCredentials ("user", "pass", null, "domain"); credentialsProvider = new BasicCredentialsProvider (); credentialsProvider.setCredentials (authscope, credentials); registry = RegistryBuilder.<AuthSchemeProvider>create () .register(AuthSchemes.NTLM, new NTLMSchemeFactory ()) .build (); HttpRequestInterceptor interceptor = (request, context) -> request.removeHeaders(HTTP.CONTENT_LEN); requestConfig = RequestConfig.custom () .setConnectTimeout (3000) .build (); HttpClient httpClient = HttpClientBuilder.create () .setDefaultRequestConfig (requestConfig) .setDefaultAuthSchemeRegistry (registry) .setDefaultCredentialsProvider (credentialsProvider) .addInterceptorFirst (interceptor) .build (); messageSender.setHttpClient (httpClient); 错误表示您的方法在代码块中放置了Not all code paths return a value语句,该语句可能无法执行。

因此,只需将returnreturn ds块移出到方法的末尾即可使其正常工作。

<强>更新

if