I have a question. There is an Excel document or Word table with fields (for example):

Full name / Zachetka / Passport / Student / Home.adr and others.

The user copies the data from the document to the clipboard. Click on the "Save" button. And the contents of the clipboard are saved to the database. How to count the contents of the buffer and bind it to the insert query?

  • one
    Brad some ... not to mention the grammar - Barmaley
  • 2
    Very witty! if you do not understand and do not know what to write, what a joke to write, that this is nonsense. Wash wrote fine, at least you can already ask again .... - Rakzin Roman

2 answers 2

In your question there is almost no information about the database, and it’s not so easy to copy the table from MS Office Word, as it seems to me, and then present it in the right form. For example, if there is a connection with the question that you asked about entering data from a table to the clipboard (where the delimiters are semicolons, and the lines are separated by a line break), you can give the following code:

  using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string[] data = Clipboard.GetText().Split('\n'); for (int i = 0; i < data.Length; i++) data[i] = data[i].Remove(data[i].Length - 1, 1); // удаляем '\r' int lengthRow = 0; for (int i = 0; i < data[0].Length; i++) if (data[0].IndexOf(';', i) != -1) lengthRow++; string insertString = "insert into " + tableName + " values("; for (int i = 0; i < lengthRow; i++) insertString += "@" + (char)(i + 97) + ", "; // 97 - a, 98 - b и т.д. insertString = insertString.Remove(insertString.Length - 2, 2); // удаляем ", " из конца insertString += ");"; SqlCommand insertCommand = new SqlCommand(insertString, connection); for (int i = 0; i < data.Length; i++) { string[] values = data[i].Split(';'); insertCommand.Parameters.Clear(); for (int j = 0; j < lengthRow; j++) insertCommand.Parameters.AddWithValue("@" + (char)(i + 97), values[j]); insertCommand.ExecuteNonQuery(); } } 

Once he coped with the task. Code on the rights of "IMHO", do not scold. Here I form the line "insert into% tableName% values ​​( @a , @b , ... @n );", then substituting specific values ​​for @a , @b and other "variables".

  • not very clear ...... table, for example, students (Zachek, Phio, student) in Excel I copy the table to the clipboard in exactly the same form (test, full, student) and I need to insert the first cell in the book ... and etc. Ie cycle, to pull out of the buffer and formed a query string .... - Rakzin Roman
  • one
    BULK INSERT can better, not? - den94
  • one
    Clipboard.GetText (). Split ('\ n'); - pulls out your text, divides it into lines. Then it goes through the rows, divides each row into an array of values, then forms an insert query and substitutes specific values ​​for the named variables, then executes the query, "clears" the values, and so on for each row. About BULK INSERT - did not understand. - MagmelS
  • one
    Curses .... The value of StartIndex can not be less than zero. Parameter name: startIndex - Rakzin Roman
  • one
    In what place? - MagmelS

The clipboard content is available via Clipboard ,

As you can see, the exchange buffer can store simultaneously (and independently): text, picture, sound, file list and general data (data) But with excel: I'm not 100% sure - when copying it, if I am not mistaken, it copies cells from the screen as a picture and saves the picture in the exchange buffer, and the very structure of cells in Data. And it is not immediately clear in what form it is there.

Therefore, if you try to insert in some paint (by the way did not try), then a picture will be inserted. And only Office will know how to decrypt what lies in data. maybe so hml, and maybe when copying text it will be read :)

By the way, I once did a program =) But on the Word, my time is over. I could create a document, but how to understand that in the exchange buffer a piece of Word / Excel / Access / etc. did not know at that time. no time