ここで例を見ています:
http://msdn.microsoft.com/en-US/library/y06xa2h1(v=vs.80).aspx
string s = "primaryKeyValue";
DataRow foundRow = dataSet1.Tables["AnyTable"].Rows.Find(s);
if (foundRow != null)
{
MessageBox.Show(foundRow[1].ToString());
}
else
{
MessageBox.Show("A row with the primary key of " + s + " could not be found");
}
彼らはどこから来たのかを指定していませんdataSet1
が、これは何らかのデータベースを表していますか?
コードでこの例を使用して一意の行を見つけようとしていますが、この構文を実装できないようです。SQLへの接続を開くために接続文字列のみを使用SqlDataAdapter
しており、機能を実行するために使用しています...
編集:
SqlConnection myConnection = new SqlConnection("Data Source=server; Initial Catalog=Dashboard; Integrated Security=SSPI; Persist Security Info=false; Trusted_Connection=Yes");
SqlDataAdapter da = new SqlDataAdapter();
try
{
//Opens the connection to the specified database
myConnection.Open();
//Specifies where the Table in the database where the data will be entered and the columns used
da.InsertCommand = new SqlCommand("INSERT INTO DashboardLibAnswer(Id,Date,Time,Question,Details,Answer,Notes,EnteredBy,WhereReceived,QuestionType,AnswerMethod,TransactionDuration)"
+ "VALUES(@Id,@Date,@Time,@Question,@Details,@Answer,@Notes,@EnteredBy,@WhereReceived,@QuestionType,@AnswerMethod,@TransactionDuration)", myConnection);
//Specifies the columns and their variable type where the data will be entered
//Special note: Conversion from String > DateTime will cause exceptions that will only import some part of data and not everything
da.InsertCommand.Parameters.Add("@Id", SqlDbType.NVarChar);
da.InsertCommand.Parameters.Add("@Date", SqlDbType.Text);
da.InsertCommand.Parameters.Add("@Time", SqlDbType.Text);
da.InsertCommand.Parameters.Add("@Question", SqlDbType.Text);
da.InsertCommand.Parameters.Add("@Details", SqlDbType.Text);
da.InsertCommand.Parameters.Add("@Answer", SqlDbType.Text);
da.InsertCommand.Parameters.Add("@Notes", SqlDbType.Text);
da.InsertCommand.Parameters.Add("@EnteredBy", SqlDbType.NVarChar);
da.InsertCommand.Parameters.Add("@WhereReceived", SqlDbType.NVarChar);
da.InsertCommand.Parameters.Add("@QuestionType", SqlDbType.NVarChar);
da.InsertCommand.Parameters.Add("@AnswerMethod", SqlDbType.NVarChar);
da.InsertCommand.Parameters.Add("@TransactionDuration", SqlDbType.NVarChar);
//Using the global variable counter this loop will go through each valid entry and insert it into the specifed database/table
for (int i = 0; i < counter; i++)
{
//Iterates through the collection array starting at first index and going through until the end
//and inserting each element into our SQL Table
DataSet dashboardDS = new DataSet();
da.Fill(dashboardDS, "DashboardLibAnswer");
DataTable dt = dashboardDS.Tables["DashboardLibAnswer"];
foreach (DataColumn col in dt.Columns)
{
if (col.Unique)
{
da.InsertCommand.Parameters["@Id"].Value = collection.getIdItems(i);
da.InsertCommand.Parameters["@Date"].Value = collection.getDateItems(i);
da.InsertCommand.Parameters["@Time"].Value = collection.getTimeItems(i);
da.InsertCommand.Parameters["@Question"].Value = collection.getQuestionItems(i);
da.InsertCommand.Parameters["@Details"].Value = collection.getDetailsItems(i);
da.InsertCommand.Parameters["@Answer"].Value = collection.getAnswerItems(i);
da.InsertCommand.Parameters["@Notes"].Value = collection.getNotesItems(i);
da.InsertCommand.Parameters["@EnteredBy"].Value = collection.getEnteredByItems(i);
da.InsertCommand.Parameters["@WhereReceived"].Value = collection.getWhereItems(i);
da.InsertCommand.Parameters["@QuestionType"].Value = collection.getQuestionTypeItems(i);
da.InsertCommand.Parameters["@AnswerMethod"].Value = collection.getAnswerMethodItems(i);
da.InsertCommand.Parameters["@TransactionDuration"].Value = collection.getTransactionItems(i);
da.InsertCommand.ExecuteNonQuery();
}
}
//Updates the progress bar using the i in addition to 1
_worker.ReportProgress(i + 1);
} // end for
//Once the importing is done it will show the appropriate message
MessageBox.Show("Finished Importing");
} // end try
catch (Exception exceptionError)
{
//To show exceptions thrown just uncomment bellow line
//rtbOutput.AppendText(exceptionError.ToString);
} // end catch
//Closes the SQL connection after importing is done
myConnection.Close();
}