问题描述
有人能帮我弄清楚如何让文本框的价值写入MS SQL数据库吗?我的数据库连接完全正常工作,现在我只需确定如何获取文本框的值并将其写入数据库。现在,下面的代码只会将TEXT字样插入到数据库中,而不是文本框的值。
function doGet(){
var app = UiApp.createApplication();
var panel = app.createAbsolutePanel();
var label = app.createLabel('Test Textbox');
var text = app.createTextBox();
var submit = app.createButton('Submit');
panel.add(label);
panel.add(text);
panel.add(submit);
app.add(panel);
//处理程序
var handler = app.createServerClickHandler('write');
handler.addCallbackElement(panel);
submit.addClickHandler(handler);
返回应用;
//将文本提交到数据库
函数write(e){
var app = UiApp.getActiveApplication();
var text = e.parameter.text;
var conn = Jdbc.getConnection(jdbc:sqlserver:// IPAddress:1433;+
databaseName = name; userusername; password = password;);
var stmt = conn.createStatement();
var sql =INSERT INTO dbo.tbl_Student(studentFName)VALUES('text');
var count = stmt.executeUpdate(sql,1)//传递任何整数以获得自动递增的ID
//检查以查看任何自动生成的键返回。这是可选的。
var rs = stmt.getGeneratedKeys();
while(rs.next()){
Logger.log(rs.getString(1));
}
rs.close();
stmt.close();
conn.close();
return app.close();
您需要为文本框创建一个名称:
修改 var text = app.createTextBox(); $> c $ c> to
var text = app.createTextBox()。setName('mytb');
。
然后,在你的处理函数中,你可以通过 var text = e.parameter.mytb;
。
另外,我认为你只是在你的数据库中插入文本字'text'。相反,尝试用这样的文本变量创建字符串: text +);
Can someone please help me figure out how to get the value of a textbox to write to a MS SQL database? I have the database connection working PERFECTLY, now I just need to determine how to grab the value of the textbox and write it to the database with Google Apps Script.
Right now the code below will only insert the word TEXT to the database and not the value of the textbox.
function doGet() {
var app = UiApp.createApplication();
var panel = app.createAbsolutePanel();
var label = app.createLabel('Test Textbox');
var text = app.createTextBox();
var submit = app.createButton('Submit');
panel.add(label);
panel.add(text);
panel.add(submit);
app.add(panel);
// Handlers
var handler = app.createServerClickHandler('write');
handler.addCallbackElement(panel);
submit.addClickHandler(handler);
return app;
}
// submit text to database
function write(e) {
var app = UiApp.getActiveApplication();
var text = e.parameter.text;
var conn = Jdbc.getConnection("jdbc:sqlserver://IPAddress:1433;" +
"databaseName=name;userusername;password=password;");
var stmt = conn.createStatement();
var sql = "INSERT INTO dbo.tbl_Student (studentFName) VALUES ('text')";
var count = stmt.executeUpdate(sql,1)//pass in any integer to get auto incremented IDs back
// check to see any auto generated keys come back. that is optional.
var rs = stmt.getGeneratedKeys();
while(rs.next()) {
Logger.log(rs.getString(1));
}
rs.close();
stmt.close();
conn.close();
return app.close();
}
In order to get the value of the textbox passed in, you need to create a name for the textbox:
Modify var text = app.createTextBox();
to var text = app.createTextBox().setName('mytb');
.
Then, in your handler function, you can retrieve this by var text = e.parameter.mytb;
.
Also, I think you are just inserting literal word 'text' into your db. Instead, try creating the string with the text variable like this:
var sql = "INSERT INTO dbo.tbl_Student (studentFName) VALUES ("+text+")";
这篇关于Google Apps脚本将文本框值插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!