###前提・実現したいこと
5852232 2A11B5632ZABFA157220000825 18:08:57 8D11080200008
上記のようなデータが1万件あるテキストファイルをデータベースに登録したいです。
1行ずつテキストファイルを読み取ってproduct_tableにINSERTしようとしてます。
SQLに触るのは初めてで右も左も分からない状態です…
何卒ご教示お願いします…
###発生している問題・エラーメッセージ
System.Data.SqlClient.SqlException (0x80131904): 文字列データまたはバイナリ データが切り捨てられます。 場所 System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
###該当のソースコード
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Data.SqlClient; 6using System.Drawing; 7using System.Linq; 8using System.Text; 9using System.Threading.Tasks; 10using System.Windows.Forms; 11using System.IO; 12using System.Drawing.Printing; 13 14namespace ProductDisplay 15{ 16 public partial class FormProductDisplay : Form 17 { 18 /// <summary> 19 /// product_table読み取りの現在位置 20 /// </summary> 21 private int location; 22 23 /// <summary> 24 /// SQLserver接続変数 25 /// </summary> 26 private SqlConnection connection; 27 28 /// <summary> 29 /// ページカウント変数 30 /// </summary> 31 private int page = 1; 32 33 /// <summary> 34 /// 印刷する最大ページ数 35 /// </summary> 36 private const int MAX_PAGE = 200; 37 38 /// <summary> 39 /// ProductDisplayフォームのコンストラクタ 40 /// </summary> 41 public FormProductDisplay() 42 { 43 InitializeComponent(); 44 } 45 46 /// <summary> 47 /// FormProductDisplayのイベントハンドラ 48 /// DBに接続してseihin.txtをINSERTする 49 /// </summary> 50 /// <param name="sender">使用しない</param> 51 /// <param name="e">使用しない</param> 52 private void FormProductDisplay_Load(object sender, EventArgs e) 53 { 54 try 55 { 56 using (connection = new SqlConnection(Properties.Settings.Default.ConnectionString)) 57 { 58 string sql = @" 59 INSERT INTO product_table ( 60 product_id, 61 basic_order_lot, 62 product_code, 63 chassis_number, 64 assembled_ymd, 65 assembled_hms, 66 struct_number, 67 yyyymm 68 ) 69 VALUES ( 70 @product_id, 71 @basic_order_lot, 72 @product_code, 73 @chassis_number, 74 @assembled_ymd, 75 @assembled_hms, 76 @struct_number, 77 @yyyymm 78 ); 79 "; 80 81 connection.Open(); 82 SqlCommand command = new SqlCommand(sql, connection); 83 try 84 { 85 StreamReader read = new StreamReader("seihin.txt"); 86 try 87 { 88 string line; 89 while ((line = read.ReadLine()) != null) 90 { 91 command.Parameters.Add(new SqlParameter("@product_id", line)); 92 command.Parameters.Add(new SqlParameter("@basic_order_lot", line)); 93 command.Parameters.Add(new SqlParameter("@product_code", line)); 94 command.Parameters.Add(new SqlParameter("@chassis_number", line)); 95 command.Parameters.Add(new SqlParameter("@assembled_ymd", line)); 96 command.Parameters.Add(new SqlParameter("@assembled_hms", line)); 97 command.Parameters.Add(new SqlParameter("@struct_number", line)); 98 command.Parameters.Add(new SqlParameter("@yyyymm", line)); 99 command.ExecuteNonQuery(); 100 } 101 read.Close(); 102 } 103 catch (Exception ex) 104 { 105 MessageBox.Show("ファイルの読み込みに失敗しました。", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); 106 System.Diagnostics.Debug.WriteLine(ex.ToString()); 107 } 108 } 109 catch (Exception ex) 110 { 111 MessageBox.Show("ファイルを開くのに失敗しました。", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); 112 System.Diagnostics.Debug.WriteLine(ex.ToString()); 113 } 114 connection.Close(); 115 } 116 } 117 catch (Exception ex) 118 { 119 MessageBox.Show("データベースに接続出来ませんでした。", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); 120 System.Diagnostics.Debug.WriteLine(ex.ToString()); 121 } 122 }
###補足情報(言語/FW/ツール等のバージョンなど)
product_tableの型はproduct_idがvarchar(11),basic_order_lotがvarchar(8),product_codeがvarchar(6),chassis_numberがvarchar(4),assembled_ymdがdate,assembled_hmsがtime(7),struct_numberがvarchar(7),yyyymmがchar(6)になってます
回答3件
あなたの回答
tips
プレビュー