0

Microsoft Visual Web Developer 2010 Express を使用しています。作成したデータベース テーブルにテキスト ボックスの入力を入力しようとしています。DataBinding プロパティ (下部の BindTextBoxes メソッドの下にあります) を使用して問題が発生しました。インターネットを検索したところ、DataBinding プロパティが Web Developer に存在しないことがわかりました。テキスト ボックスの入力をデータベース テーブルに転送する別の方法はありますか?

これが私のコードのデータベース部分です(C#で):

    namespace WebApplication2
    {
        public partial class _Default : System.Web.UI.Page
        {
     //used to link textboxes with database
     BindingSource bsUserDetails = new BindingSource();

     //info stored in tables
     DataSet dsUserDetails = new DataSet();

     //manages connection between database and application
     SqlDataAdapter daUserDetails = new SqlDataAdapter();

     //create new SQL connection
     SqlConnection connUserDetails = new SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Users\synthesis\Documents\Visual Studio 2010\Projects\Practical\Practical\App_Data\UserDetails.mdf;Integrated Security=True;User Instance=True");

     protected void Page_Load(object sender, EventArgs e)
     {
         //link textboxes to relevant fields in the dataset
         bsUserDetails.DataSource = dsUserDetails;
         bsUserDetails.DataMember = dsUserDetails.Tables[0].ToString();

         //call BindTextBoxes Method
         BindTextBoxes();

     private void BindTextBoxes()
     {
     txtBoxCell.DataBindings.Add(new Binding("Name entered into txtBox", bsUserDetails,              
     "Name column in database table", true));
     }
   }
 }
4

1 に答える 1

0

テキスト ボックスからデータを挿入するための独自の SQL をいつでも作成できます。SqlConnection および SqlCommand オブジェクトをセットアップする必要があります。次に、コマンドで SQL を定義し、パラメーターを設定して SQL インジェクションを防ぐ必要があります。このようなもの:

StringBuilder sb = new StringBuilder();
sb.Append("INSERT INTO sometable VALUES(@text1,@text2)");

SqlConnection conn = new SqlConnection(connStr);
SqlCommand command = new SqlCommand(sb.ToString());
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("text1", text1.Text);
command.Parameters.AddWithValue("text2", text2.Text);
command.ExecuteNonQuery();
于 2013-04-01T17:45:20.630 に答える